25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

ClientFlowTest.cs 1.4 KiB

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