Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

97 linhas
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.Options;
  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 { Topic = "xyz", QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce });
  41. await managedClient.SubscribeAsync(new TopicFilter { Topic = "abc", QualityOfServiceLevel = 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 => Guid.NewGuid().ToString();
  54. public string Username => "the_static_user";
  55. }
  56. public class ClientRetainedMessageHandler : IManagedMqttClientStorage
  57. {
  58. private const string Filename = @"RetainedMessages.json";
  59. public Task SaveQueuedMessagesAsync(IList<ManagedMqttApplicationMessage> messages)
  60. {
  61. File.WriteAllText(Filename, JsonConvert.SerializeObject(messages));
  62. return Task.FromResult(0);
  63. }
  64. public Task<IList<ManagedMqttApplicationMessage>> LoadQueuedMessagesAsync()
  65. {
  66. IList<ManagedMqttApplicationMessage> retainedMessages;
  67. if (File.Exists(Filename))
  68. {
  69. var json = File.ReadAllText(Filename);
  70. retainedMessages = JsonConvert.DeserializeObject<List<ManagedMqttApplicationMessage>>(json);
  71. }
  72. else
  73. {
  74. retainedMessages = new List<ManagedMqttApplicationMessage>();
  75. }
  76. return Task.FromResult(retainedMessages);
  77. }
  78. }
  79. }
  80. }