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.
 
 
 
 

100 lines
3.5 KiB

  1. using MQTTnet.Client;
  2. using MQTTnet.Client.Connecting;
  3. using MQTTnet.Client.Disconnecting;
  4. using MQTTnet.Client.Options;
  5. using MQTTnet.Client.Receiving;
  6. using MQTTnet.Protocol;
  7. using System;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using MQTTnet.Diagnostics;
  11. namespace MQTTnet.TestApp.NetCore
  12. {
  13. public static class ClientTest
  14. {
  15. public static async Task RunAsync()
  16. {
  17. try
  18. {
  19. var logger = new MqttNetLogger();
  20. MqttNetConsoleLogger.ForwardToConsole(logger);
  21. var factory = new MqttFactory(logger);
  22. var client = factory.CreateMqttClient();
  23. var clientOptions = new MqttClientOptions
  24. {
  25. ChannelOptions = new MqttClientTcpOptions
  26. {
  27. Server = "127.0.0.1"
  28. }
  29. };
  30. client.ApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(e =>
  31. {
  32. Console.WriteLine("### RECEIVED APPLICATION MESSAGE ###");
  33. Console.WriteLine($"+ Topic = {e.ApplicationMessage.Topic}");
  34. Console.WriteLine($"+ Payload = {Encoding.UTF8.GetString(e.ApplicationMessage.Payload)}");
  35. Console.WriteLine($"+ QoS = {e.ApplicationMessage.QualityOfServiceLevel}");
  36. Console.WriteLine($"+ Retain = {e.ApplicationMessage.Retain}");
  37. Console.WriteLine();
  38. });
  39. client.ConnectedHandler = new MqttClientConnectedHandlerDelegate(async e =>
  40. {
  41. Console.WriteLine("### CONNECTED WITH SERVER ###");
  42. await client.SubscribeAsync(new MqttTopicFilterBuilder().WithTopic("#").Build());
  43. Console.WriteLine("### SUBSCRIBED ###");
  44. });
  45. client.DisconnectedHandler = new MqttClientDisconnectedHandlerDelegate(async e =>
  46. {
  47. Console.WriteLine("### DISCONNECTED FROM SERVER ###");
  48. await Task.Delay(TimeSpan.FromSeconds(5));
  49. try
  50. {
  51. await client.ConnectAsync(clientOptions);
  52. }
  53. catch
  54. {
  55. Console.WriteLine("### RECONNECTING FAILED ###");
  56. }
  57. });
  58. try
  59. {
  60. await client.ConnectAsync(clientOptions);
  61. }
  62. catch (Exception exception)
  63. {
  64. Console.WriteLine("### CONNECTING FAILED ###" + Environment.NewLine + exception);
  65. }
  66. Console.WriteLine("### WAITING FOR APPLICATION MESSAGES ###");
  67. while (true)
  68. {
  69. Console.ReadLine();
  70. await client.SubscribeAsync(new MqttTopicFilter { Topic = "test", QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce });
  71. var applicationMessage = new MqttApplicationMessageBuilder()
  72. .WithTopic("A/B/C")
  73. .WithPayload("Hello World")
  74. .WithAtLeastOnceQoS()
  75. .Build();
  76. await client.PublishAsync(applicationMessage);
  77. }
  78. }
  79. catch (Exception exception)
  80. {
  81. Console.WriteLine(exception);
  82. }
  83. }
  84. }
  85. }