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.
 
 
 
 

192 lines
6.4 KiB

  1. using MQTTnet.Client.Options;
  2. using MQTTnet.Diagnostics;
  3. using MQTTnet.Server;
  4. using Newtonsoft.Json;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Net.Security;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using MQTTnet.Client;
  12. using MQTTnet.Diagnostics.Runtime;
  13. namespace MQTTnet.TestApp.NetCore
  14. {
  15. public static class Program
  16. {
  17. public static void Main()
  18. {
  19. //MqttNetConsoleLogger.ForwardToConsole();
  20. Console.WriteLine($"MQTTnet - TestApp.{TargetFrameworkProvider.TargetFramework}");
  21. Console.WriteLine("1 = Start client");
  22. Console.WriteLine("2 = Start server");
  23. Console.WriteLine("3 = Start performance test");
  24. Console.WriteLine("4 = Start managed client");
  25. Console.WriteLine("5 = Start public broker test");
  26. Console.WriteLine("6 = Start server & client");
  27. Console.WriteLine("7 = Client flow test");
  28. Console.WriteLine("8 = Start performance test (client only)");
  29. Console.WriteLine("9 = Start server (no trace)");
  30. Console.WriteLine("a = Start QoS 2 benchmark");
  31. Console.WriteLine("b = Start QoS 1 benchmark");
  32. Console.WriteLine("c = Start QoS 0 benchmark");
  33. Console.WriteLine("d = Start server with logging");
  34. var pressedKey = Console.ReadKey(true);
  35. if (pressedKey.KeyChar == '1')
  36. {
  37. Task.Run(ClientTest.RunAsync);
  38. }
  39. else if (pressedKey.KeyChar == '2')
  40. {
  41. Task.Run(ServerTest.RunAsync);
  42. }
  43. else if (pressedKey.KeyChar == '3')
  44. {
  45. Task.Run(PerformanceTest.RunClientAndServer);
  46. }
  47. else if (pressedKey.KeyChar == '4')
  48. {
  49. Task.Run(ManagedClientTest.RunAsync);
  50. }
  51. else if (pressedKey.KeyChar == '5')
  52. {
  53. Task.Run(PublicBrokerTest.RunAsync);
  54. }
  55. else if (pressedKey.KeyChar == '6')
  56. {
  57. Task.Run(ServerAndClientTest.RunAsync);
  58. }
  59. else if (pressedKey.KeyChar == '7')
  60. {
  61. Task.Run(ClientFlowTest.RunAsync);
  62. }
  63. else if (pressedKey.KeyChar == '8')
  64. {
  65. PerformanceTest.RunClientOnly();
  66. return;
  67. }
  68. else if (pressedKey.KeyChar == '9')
  69. {
  70. ServerTest.RunEmptyServer();
  71. return;
  72. }
  73. else if (pressedKey.KeyChar == 'a')
  74. {
  75. Task.Run(PerformanceTest.RunQoS2Test);
  76. }
  77. else if (pressedKey.KeyChar == 'b')
  78. {
  79. Task.Run(PerformanceTest.RunQoS1Test);
  80. }
  81. else if (pressedKey.KeyChar == 'c')
  82. {
  83. Task.Run(PerformanceTest.RunQoS0Test);
  84. }
  85. else if (pressedKey.KeyChar == 'd')
  86. {
  87. Task.Run(ServerTest.RunEmptyServerWithLogging);
  88. }
  89. Thread.Sleep(Timeout.Infinite);
  90. }
  91. static int _count;
  92. static async Task ClientTestWithHandlers()
  93. {
  94. //private static int _count = 0;
  95. var factory = new MqttFactory();
  96. var mqttClient = factory.CreateMqttClient();
  97. var options = new MqttClientOptionsBuilder()
  98. .WithClientId("mqttnetspeed")
  99. .WithTcpServer("#serveraddress#")
  100. .WithCredentials("#username#", "#password#")
  101. .WithCleanSession()
  102. .Build();
  103. //mqttClient.ApplicationMessageReceived += (s, e) => // version 2.8.5
  104. mqttClient.UseApplicationMessageReceivedHandler(e => // version 3.0.0+
  105. {
  106. Interlocked.Increment(ref _count);
  107. });
  108. //mqttClient.Connected += async (s, e) => // version 2.8.5
  109. mqttClient.UseConnectedHandler(async e => // version 3.0.0+
  110. {
  111. Console.WriteLine("### CONNECTED WITH SERVER ###");
  112. await mqttClient.SubscribeAsync("topic/+", MQTTnet.Protocol.MqttQualityOfServiceLevel.AtLeastOnce);
  113. Console.WriteLine("### SUBSCRIBED ###");
  114. });
  115. await mqttClient.ConnectAsync(options);
  116. while (true)
  117. {
  118. Console.WriteLine($"{Interlocked.Exchange(ref _count, 0)}/s");
  119. await Task.Delay(TimeSpan.FromSeconds(1));
  120. }
  121. }
  122. }
  123. public class RetainedMessageHandler : IMqttServerStorage
  124. {
  125. const string Filename = "C:\\MQTT\\RetainedMessages.json";
  126. public Task SaveRetainedMessagesAsync(IList<MqttApplicationMessage> messages)
  127. {
  128. var directory = Path.GetDirectoryName(Filename);
  129. if (!Directory.Exists(directory))
  130. {
  131. Directory.CreateDirectory(directory);
  132. }
  133. File.WriteAllText(Filename, JsonConvert.SerializeObject(messages));
  134. return Task.FromResult(0);
  135. }
  136. public Task<IList<MqttApplicationMessage>> LoadRetainedMessagesAsync()
  137. {
  138. IList<MqttApplicationMessage> retainedMessages;
  139. if (File.Exists(Filename))
  140. {
  141. var json = File.ReadAllText(Filename);
  142. retainedMessages = JsonConvert.DeserializeObject<List<MqttApplicationMessage>>(json);
  143. }
  144. else
  145. {
  146. retainedMessages = new List<MqttApplicationMessage>();
  147. }
  148. return Task.FromResult(retainedMessages);
  149. }
  150. }
  151. public class WikiCode
  152. {
  153. public void Code()
  154. {
  155. //Validate certificate.
  156. var options = new MqttClientOptionsBuilder()
  157. .WithTls(new MqttClientOptionsBuilderTlsParameters
  158. {
  159. CertificateValidationHandler = context =>
  160. {
  161. // TODO: Check conditions of certificate by using above context.
  162. if (context.SslPolicyErrors == SslPolicyErrors.None)
  163. {
  164. return true;
  165. }
  166. return false;
  167. }
  168. })
  169. .Build();
  170. }
  171. }
  172. }