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.
 
 
 
 

103 rivejä
3.4 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.Extensions.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(builder => builder.WithTopic("Step").WithPayload("1"));
  38. await managedClient.PublishAsync(builder => builder.WithTopic("Step").WithPayload("2").WithAtLeastOnceQoS());
  39. await managedClient.StartAsync(options);
  40. await managedClient.SubscribeAsync(new TopicFilter("xyz", MqttQualityOfServiceLevel.AtMostOnce));
  41. await managedClient.SubscribeAsync(new TopicFilter("abc", MqttQualityOfServiceLevel.AtMostOnce));
  42. await managedClient.PublishAsync(builder => builder.WithTopic("Step").WithPayload("3"));
  43. Console.WriteLine("Managed client started.");
  44. Console.ReadLine();
  45. }
  46. catch (Exception e)
  47. {
  48. Console.WriteLine(e);
  49. }
  50. }
  51. public class RandomPassword : IMqttClientCredentials
  52. {
  53. public string Password
  54. {
  55. get
  56. {
  57. return Guid.NewGuid().ToString(); // The random password.
  58. }
  59. }
  60. public string Username => "the_static_user";
  61. }
  62. public class ClientRetainedMessageHandler : IManagedMqttClientStorage
  63. {
  64. private const string Filename = @"RetainedMessages.json";
  65. public Task SaveQueuedMessagesAsync(IList<ManagedMqttApplicationMessage> messages)
  66. {
  67. File.WriteAllText(Filename, JsonConvert.SerializeObject(messages));
  68. return Task.FromResult(0);
  69. }
  70. public Task<IList<ManagedMqttApplicationMessage>> LoadQueuedMessagesAsync()
  71. {
  72. IList<ManagedMqttApplicationMessage> retainedMessages;
  73. if (File.Exists(Filename))
  74. {
  75. var json = File.ReadAllText(Filename);
  76. retainedMessages = JsonConvert.DeserializeObject<List<ManagedMqttApplicationMessage>>(json);
  77. }
  78. else
  79. {
  80. retainedMessages = new List<ManagedMqttApplicationMessage>();
  81. }
  82. return Task.FromResult(retainedMessages);
  83. }
  84. }
  85. }
  86. }