You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

53 lines
1.6 KiB

  1. using System;
  2. using System.Threading.Tasks;
  3. using MQTTnet.Client;
  4. using MQTTnet.Client.Options;
  5. using MQTTnet.Diagnostics;
  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 MqttNetLogger();
  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. }