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.
 
 
 
 

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