You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

102 line
3.3 KiB

  1. using System;
  2. using System.Threading.Tasks;
  3. using System.IO;
  4. using Newtonsoft.Json;
  5. using System.Collections.Generic;
  6. using MQTTnet.Client;
  7. using MQTTnet.ManagedClient;
  8. using MQTTnet.Protocol;
  9. namespace MQTTnet.TestApp.NetCore
  10. {
  11. public static class ManagedClientTest
  12. {
  13. public static async Task RunAsync()
  14. {
  15. var ms = new ClientRetainedMessageHandler();
  16. var options = new ManagedMqttClientOptions
  17. {
  18. ClientOptions = new MqttClientOptions
  19. {
  20. ClientId = "MQTTnetManagedClientTest",
  21. Credentials = new RandomPassword(),
  22. ChannelOptions = new MqttClientTcpOptions
  23. {
  24. Server = "broker.hivemq.com"
  25. }
  26. },
  27. AutoReconnectDelay = TimeSpan.FromSeconds(1),
  28. Storage = ms
  29. };
  30. try
  31. {
  32. var managedClient = new MqttFactory().CreateManagedMqttClient();
  33. managedClient.ApplicationMessageReceived += (s, e) =>
  34. {
  35. Console.WriteLine(">> RECEIVED: " + e.ApplicationMessage.Topic);
  36. };
  37. await managedClient.PublishAsync(new MqttApplicationMessageBuilder().WithTopic("Step").WithPayload("1").Build());
  38. await managedClient.PublishAsync(new MqttApplicationMessageBuilder().WithTopic("Step").WithPayload("2").WithAtLeastOnceQoS().Build());
  39. await managedClient.StartAsync(options);
  40. await managedClient.SubscribeAsync(new TopicFilter("xyz", MqttQualityOfServiceLevel.AtMostOnce));
  41. await managedClient.PublishAsync(new MqttApplicationMessageBuilder().WithTopic("Step").WithPayload("3").Build());
  42. Console.WriteLine("Managed client started.");
  43. Console.ReadLine();
  44. }
  45. catch (Exception e)
  46. {
  47. Console.WriteLine(e);
  48. }
  49. }
  50. public class RandomPassword : IMqttClientCredentials
  51. {
  52. public string Password
  53. {
  54. get
  55. {
  56. return Guid.NewGuid().ToString(); // The random password.
  57. }
  58. }
  59. public string Username => "the_static_user";
  60. }
  61. public class ClientRetainedMessageHandler : IManagedMqttClientStorage
  62. {
  63. private const string Filename = @"RetainedMessages.json";
  64. public Task SaveQueuedMessagesAsync(IList<MqttApplicationMessage> messages)
  65. {
  66. File.WriteAllText(Filename, JsonConvert.SerializeObject(messages));
  67. return Task.FromResult(0);
  68. }
  69. public Task<IList<MqttApplicationMessage>> LoadQueuedMessagesAsync()
  70. {
  71. IList<MqttApplicationMessage> retainedMessages;
  72. if (File.Exists(Filename))
  73. {
  74. var json = File.ReadAllText(Filename);
  75. retainedMessages = JsonConvert.DeserializeObject<List<MqttApplicationMessage>>(json);
  76. }
  77. else
  78. {
  79. retainedMessages = new List<MqttApplicationMessage>();
  80. }
  81. return Task.FromResult(retainedMessages);
  82. }
  83. }
  84. }
  85. }