Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ManagedClientTest.cs 3.4 KiB

před 6 roky
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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.Options;
  7. using MQTTnet.Client.Receiving;
  8. using MQTTnet.Extensions.ManagedClient;
  9. using MQTTnet.Protocol;
  10. namespace MQTTnet.TestApp.NetCore
  11. {
  12. public static class ManagedClientTest
  13. {
  14. public static async Task RunAsync()
  15. {
  16. var ms = new ClientRetainedMessageHandler();
  17. var options = new ManagedMqttClientOptions
  18. {
  19. ClientOptions = new MqttClientOptions
  20. {
  21. ClientId = "MQTTnetManagedClientTest",
  22. Credentials = new RandomPassword(),
  23. ChannelOptions = new MqttClientTcpOptions
  24. {
  25. Server = "broker.hivemq.com"
  26. }
  27. },
  28. AutoReconnectDelay = TimeSpan.FromSeconds(1),
  29. Storage = ms
  30. };
  31. try
  32. {
  33. var managedClient = new MqttFactory().CreateManagedMqttClient();
  34. managedClient.ApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(e =>
  35. {
  36. Console.WriteLine(">> RECEIVED: " + e.ApplicationMessage.Topic);
  37. });
  38. await managedClient.PublishAsync(builder => builder.WithTopic("Step").WithPayload("1"));
  39. await managedClient.PublishAsync(builder => builder.WithTopic("Step").WithPayload("2").WithAtLeastOnceQoS());
  40. await managedClient.StartAsync(options);
  41. await managedClient.SubscribeAsync(new TopicFilter { Topic = "xyz", QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce });
  42. await managedClient.SubscribeAsync(new TopicFilter { Topic = "abc", QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce });
  43. await managedClient.PublishAsync(builder => builder.WithTopic("Step").WithPayload("3"));
  44. Console.WriteLine("Managed client started.");
  45. Console.ReadLine();
  46. }
  47. catch (Exception e)
  48. {
  49. Console.WriteLine(e);
  50. }
  51. }
  52. public class RandomPassword : IMqttClientCredentials
  53. {
  54. public byte[] Password => Guid.NewGuid().ToByteArray();
  55. public string Username => "the_static_user";
  56. }
  57. public class ClientRetainedMessageHandler : IManagedMqttClientStorage
  58. {
  59. private const string Filename = @"RetainedMessages.json";
  60. public Task SaveQueuedMessagesAsync(IList<ManagedMqttApplicationMessage> messages)
  61. {
  62. File.WriteAllText(Filename, JsonConvert.SerializeObject(messages));
  63. return Task.FromResult(0);
  64. }
  65. public Task<IList<ManagedMqttApplicationMessage>> LoadQueuedMessagesAsync()
  66. {
  67. IList<ManagedMqttApplicationMessage> retainedMessages;
  68. if (File.Exists(Filename))
  69. {
  70. var json = File.ReadAllText(Filename);
  71. retainedMessages = JsonConvert.DeserializeObject<List<ManagedMqttApplicationMessage>>(json);
  72. }
  73. else
  74. {
  75. retainedMessages = new List<ManagedMqttApplicationMessage>();
  76. }
  77. return Task.FromResult(retainedMessages);
  78. }
  79. }
  80. }
  81. }