Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

103 righe
3.5 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.SubscribeAsync(new TopicFilter("abc", MqttQualityOfServiceLevel.AtMostOnce));
  42. await managedClient.PublishAsync(new MqttApplicationMessageBuilder().WithTopic("Step").WithPayload("3").Build());
  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<MqttApplicationMessage> messages)
  66. {
  67. File.WriteAllText(Filename, JsonConvert.SerializeObject(messages));
  68. return Task.FromResult(0);
  69. }
  70. public Task<IList<MqttApplicationMessage>> LoadQueuedMessagesAsync()
  71. {
  72. IList<MqttApplicationMessage> retainedMessages;
  73. if (File.Exists(Filename))
  74. {
  75. var json = File.ReadAllText(Filename);
  76. retainedMessages = JsonConvert.DeserializeObject<List<MqttApplicationMessage>>(json);
  77. }
  78. else
  79. {
  80. retainedMessages = new List<MqttApplicationMessage>();
  81. }
  82. return Task.FromResult(retainedMessages);
  83. }
  84. }
  85. }
  86. }