Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

170 righe
5.6 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using MQTTnet.Core;
  7. using MQTTnet.Core.Client;
  8. using MQTTnet.Core.Diagnostics;
  9. using MQTTnet.Core.Packets;
  10. using MQTTnet.Core.Protocol;
  11. using MQTTnet.Core.Server;
  12. namespace MQTTnet.TestApp.NetFramework
  13. {
  14. public static class Program
  15. {
  16. public static void Main(string[] args)
  17. {
  18. Console.WriteLine("MQTTnet - TestApp.NetFramework");
  19. Console.WriteLine("1 = Start client");
  20. Console.WriteLine("2 = Start server");
  21. Console.WriteLine("3 = Start performance test");
  22. var pressedKey = Console.ReadKey(true);
  23. if (pressedKey.Key == ConsoleKey.D1)
  24. {
  25. Task.Run(() => RunClientAsync(args));
  26. Thread.Sleep(Timeout.Infinite);
  27. }
  28. else if (pressedKey.Key == ConsoleKey.D2)
  29. {
  30. Task.Run(() => RunServerAsync(args));
  31. Thread.Sleep(Timeout.Infinite);
  32. }
  33. else if (pressedKey.Key == ConsoleKey.D3)
  34. {
  35. Task.Run(PerformanceTest.RunAsync);
  36. Thread.Sleep(Timeout.Infinite);
  37. }
  38. }
  39. private static async Task RunClientAsync(string[] arguments)
  40. {
  41. MqttTrace.TraceMessagePublished += (s, e) =>
  42. {
  43. Console.WriteLine($">> [{e.ThreadId}] [{e.Source}] [{e.Level}]: {e.Message}");
  44. if (e.Exception != null)
  45. {
  46. Console.WriteLine(e.Exception);
  47. }
  48. };
  49. try
  50. {
  51. var options = new MqttClientOptions
  52. {
  53. Server = "localhost",
  54. ClientId = "XYZ",
  55. CleanSession = true
  56. };
  57. var client = new MqttClientFactory().CreateMqttClient(options);
  58. client.ApplicationMessageReceived += (s, e) =>
  59. {
  60. Console.WriteLine("### RECEIVED APPLICATION MESSAGE ###");
  61. Console.WriteLine($"+ Topic = {e.ApplicationMessage.Topic}");
  62. Console.WriteLine($"+ Payload = {Encoding.UTF8.GetString(e.ApplicationMessage.Payload)}");
  63. Console.WriteLine($"+ QoS = {e.ApplicationMessage.QualityOfServiceLevel}");
  64. Console.WriteLine($"+ Retain = {e.ApplicationMessage.Retain}");
  65. Console.WriteLine();
  66. };
  67. client.Connected += async (s, e) =>
  68. {
  69. Console.WriteLine("### CONNECTED WITH SERVER ###");
  70. await client.SubscribeAsync(new List<TopicFilter>
  71. {
  72. new TopicFilter("#", MqttQualityOfServiceLevel.AtMostOnce)
  73. });
  74. };
  75. client.Disconnected += async (s, e) =>
  76. {
  77. Console.WriteLine("### DISCONNECTED FROM SERVER ###");
  78. await Task.Delay(TimeSpan.FromSeconds(5));
  79. try
  80. {
  81. await client.ConnectAsync();
  82. }
  83. catch
  84. {
  85. Console.WriteLine("### RECONNECTING FAILED ###");
  86. }
  87. };
  88. try
  89. {
  90. await client.ConnectAsync();
  91. }
  92. catch (Exception exception)
  93. {
  94. Console.WriteLine("### CONNECTING FAILED ###" + Environment.NewLine + exception);
  95. }
  96. Console.WriteLine("### WAITING FOR APPLICATION MESSAGES ###");
  97. var messageFactory = new MqttApplicationMessageFactory();
  98. while (true)
  99. {
  100. Console.ReadLine();
  101. var applicationMessage = messageFactory.CreateApplicationMessage("myTopic", "Hello World", MqttQualityOfServiceLevel.AtLeastOnce);
  102. await client.PublishAsync(applicationMessage);
  103. }
  104. }
  105. catch (Exception exception)
  106. {
  107. Console.WriteLine(exception);
  108. }
  109. }
  110. private static void RunServerAsync(string[] arguments)
  111. {
  112. MqttTrace.TraceMessagePublished += (s, e) =>
  113. {
  114. Console.WriteLine($">> [{e.ThreadId}] [{e.Source}] [{e.Level}]: {e.Message}");
  115. if (e.Exception != null)
  116. {
  117. Console.WriteLine(e.Exception);
  118. }
  119. };
  120. try
  121. {
  122. var options = new MqttServerOptions
  123. {
  124. ConnectionValidator = p =>
  125. {
  126. if (p.ClientId == "SpecialClient")
  127. {
  128. if (p.Username != "USER" || p.Password != "PASS")
  129. {
  130. return MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword;
  131. }
  132. }
  133. return MqttConnectReturnCode.ConnectionAccepted;
  134. }
  135. };
  136. var mqttServer = new MqttServerFactory().CreateMqttServer(options);
  137. mqttServer.Start();
  138. Console.WriteLine("Press any key to exit.");
  139. Console.ReadLine();
  140. mqttServer.Stop();
  141. }
  142. catch (Exception e)
  143. {
  144. Console.WriteLine(e);
  145. }
  146. Console.ReadLine();
  147. }
  148. }
  149. }