Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

63 строки
2.2 KiB

  1. using System;
  2. using System.Threading.Tasks;
  3. using MQTTnet.Core;
  4. using MQTTnet.Core.Client;
  5. using MQTTnet.Core.Diagnostics;
  6. using MQTTnet.Core.ManagedClient;
  7. using MQTTnet.Core.Packets;
  8. using MQTTnet.Core.Protocol;
  9. namespace MQTTnet.TestApp.NetCore
  10. {
  11. public static class ManagedClientTest
  12. {
  13. public static async Task RunAsync()
  14. {
  15. MqttNetTrace.TraceMessagePublished += (s, e) =>
  16. {
  17. Console.WriteLine($">> [{e.TraceMessage.Timestamp:O}] [{e.TraceMessage.ThreadId}] [{e.TraceMessage.Source}] [{e.TraceMessage.Level}]: {e.TraceMessage.Message}");
  18. if (e.TraceMessage.Exception != null)
  19. {
  20. Console.WriteLine(e.TraceMessage.Exception);
  21. }
  22. };
  23. var options = new ManagedMqttClientOptions
  24. {
  25. ClientOptions = new MqttClientTcpOptions
  26. {
  27. Server = "broker.hivemq.com",
  28. ClientId = "MQTTnetManagedClientTest"
  29. },
  30. AutoReconnectDelay = TimeSpan.FromSeconds(1)
  31. };
  32. try
  33. {
  34. var managedClient = new MqttClientFactory().CreateManagedMqttClient();
  35. managedClient.ApplicationMessageReceived += (s, e) =>
  36. {
  37. Console.WriteLine(">> RECEIVED: " + e.ApplicationMessage.Topic);
  38. };
  39. await managedClient.EnqueueAsync(new MqttApplicationMessageBuilder().WithTopic("Step").WithPayload("1").Build());
  40. await managedClient.EnqueueAsync(new MqttApplicationMessageBuilder().WithTopic("Step").WithPayload("2").WithAtLeastOnceQoS().Build());
  41. await managedClient.StartAsync(options);
  42. await managedClient.SubscribeAsync(new TopicFilter("xyz", MqttQualityOfServiceLevel.AtMostOnce));
  43. await managedClient.EnqueueAsync(new MqttApplicationMessageBuilder().WithTopic("Step").WithPayload("3").Build());
  44. Console.WriteLine("Managed client started.");
  45. Console.ReadLine();
  46. }
  47. catch (Exception e)
  48. {
  49. Console.WriteLine(e);
  50. }
  51. }
  52. }
  53. }