Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ClientFlowTest.cs 1.6 KiB

6 år sedan
6 år sedan
6 år sedan
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. }