您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

53 行
1.6 KiB

  1. using System;
  2. using System.Threading.Tasks;
  3. using MQTTnet.Client;
  4. using MQTTnet.Client.Options;
  5. using MQTTnet.Diagnostics.Logger;
  6. namespace MQTTnet.TestApp.NetCore
  7. {
  8. public static class ClientFlowTest
  9. {
  10. public static async Task RunAsync()
  11. {
  12. try
  13. {
  14. var logger = new MqttNetEventLogger();
  15. MqttNetConsoleLogger.ForwardToConsole(logger);
  16. var factory = new MqttFactory(logger);
  17. var client = factory.CreateMqttClient();
  18. var options = new MqttClientOptionsBuilder()
  19. .WithTcpServer("localhost")
  20. .Build();
  21. Console.WriteLine("BEFORE CONNECT");
  22. await client.ConnectAsync(options);
  23. Console.WriteLine("AFTER CONNECT");
  24. Console.WriteLine("BEFORE SUBSCRIBE");
  25. await client.SubscribeAsync("test/topic");
  26. Console.WriteLine("AFTER SUBSCRIBE");
  27. Console.WriteLine("BEFORE PUBLISH");
  28. await client.PublishAsync("test/topic", "payload");
  29. Console.WriteLine("AFTER PUBLISH");
  30. await Task.Delay(1000);
  31. Console.WriteLine("BEFORE DISCONNECT");
  32. await client.DisconnectAsync();
  33. Console.WriteLine("AFTER DISCONNECT");
  34. Console.WriteLine("FINISHED");
  35. }
  36. catch (Exception ex)
  37. {
  38. Console.WriteLine(ex);
  39. }
  40. }
  41. }
  42. }