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.
 
 
 
 

173 rader
5.8 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using MQTTnet.Client;
  7. using MQTTnet.Client.Options;
  8. using MQTTnet.Diagnostics;
  9. using MQTTnet.Server;
  10. using Newtonsoft.Json;
  11. namespace MQTTnet.TestApp.NetCore
  12. {
  13. public static class Program
  14. {
  15. public static void Main()
  16. {
  17. Console.WriteLine($"MQTTnet - TestApp.{TargetFrameworkInfoProvider.TargetFramework}");
  18. Console.WriteLine("1 = Start client");
  19. Console.WriteLine("2 = Start server");
  20. Console.WriteLine("3 = Start performance test");
  21. Console.WriteLine("4 = Start managed client");
  22. Console.WriteLine("5 = Start public broker test");
  23. Console.WriteLine("6 = Start server & client");
  24. Console.WriteLine("7 = Client flow test");
  25. Console.WriteLine("8 = Start performance test (client only)");
  26. Console.WriteLine("9 = Start server (no trace)");
  27. Console.WriteLine("a = Start QoS 2 benchmark");
  28. Console.WriteLine("b = Start QoS 1 benchmark");
  29. Console.WriteLine("c = Start QoS 0 benchmark");
  30. var pressedKey = Console.ReadKey(true);
  31. if (pressedKey.KeyChar == '1')
  32. {
  33. Task.Run(ClientTest.RunAsync);
  34. }
  35. else if (pressedKey.KeyChar == '2')
  36. {
  37. Task.Run(ServerTest.RunAsync);
  38. }
  39. else if (pressedKey.KeyChar == '3')
  40. {
  41. PerformanceTest.RunClientAndServer();
  42. return;
  43. }
  44. else if (pressedKey.KeyChar == '4')
  45. {
  46. Task.Run(ManagedClientTest.RunAsync);
  47. }
  48. else if (pressedKey.KeyChar == '5')
  49. {
  50. Task.Run(PublicBrokerTest.RunAsync);
  51. }
  52. else if (pressedKey.KeyChar == '6')
  53. {
  54. Task.Run(ServerAndClientTest.RunAsync);
  55. }
  56. else if (pressedKey.KeyChar == '7')
  57. {
  58. Task.Run(ClientFlowTest.RunAsync);
  59. }
  60. else if (pressedKey.KeyChar == '8')
  61. {
  62. PerformanceTest.RunClientOnly();
  63. return;
  64. }
  65. else if (pressedKey.KeyChar == '9')
  66. {
  67. ServerTest.RunEmptyServer();
  68. return;
  69. }
  70. else if (pressedKey.KeyChar == 'a')
  71. {
  72. Task.Run(PerformanceTest.RunQoS2Test);
  73. }
  74. else if (pressedKey.KeyChar == 'b')
  75. {
  76. Task.Run(PerformanceTest.RunQoS1Test);
  77. }
  78. else if (pressedKey.KeyChar == 'c')
  79. {
  80. Task.Run(PerformanceTest.RunQoS0Test);
  81. }
  82. Thread.Sleep(Timeout.Infinite);
  83. }
  84. // This code is used at the Wiki on GitHub!
  85. // ReSharper disable once UnusedMember.Local
  86. private static async void WikiCode()
  87. {
  88. {
  89. var client = new MqttFactory().CreateMqttClient();
  90. var options = new MqttClientOptionsBuilder()
  91. .WithClientId("Client1")
  92. .WithTcpServer("broker.hivemq.com")
  93. .WithCredentials("bud", "%spencer%")
  94. .WithTls()
  95. .Build();
  96. await client.ConnectAsync(options);
  97. var message = new MqttApplicationMessageBuilder()
  98. .WithTopic("MyTopic")
  99. .WithPayload("Hello World")
  100. .WithExactlyOnceQoS()
  101. .WithRetainFlag()
  102. .Build();
  103. await client.PublishAsync(message);
  104. }
  105. {
  106. var factory = new MqttFactory();
  107. var client = factory.CreateMqttClient();
  108. }
  109. {
  110. // Write all trace messages to the console window.
  111. MqttNetGlobalLogger.LogMessagePublished += (s, e) =>
  112. {
  113. var trace = $">> [{e.TraceMessage.Timestamp:O}] [{e.TraceMessage.ThreadId}] [{e.TraceMessage.Source}] [{e.TraceMessage.Level}]: {e.TraceMessage.Message}";
  114. if (e.TraceMessage.Exception != null)
  115. {
  116. trace += Environment.NewLine + e.TraceMessage.Exception.ToString();
  117. }
  118. Console.WriteLine(trace);
  119. };
  120. }
  121. {
  122. // Use a custom log ID for the logger.
  123. var factory = new MqttFactory();
  124. var mqttClient = factory.CreateMqttClient(new MqttNetLogger("MyCustomId"));
  125. }
  126. }
  127. }
  128. public class RetainedMessageHandler : IMqttServerStorage
  129. {
  130. private const string Filename = "C:\\MQTT\\RetainedMessages.json";
  131. public Task SaveRetainedMessagesAsync(IList<MqttApplicationMessage> messages)
  132. {
  133. var directory = Path.GetDirectoryName(Filename);
  134. if (!Directory.Exists(directory))
  135. {
  136. Directory.CreateDirectory(directory);
  137. }
  138. File.WriteAllText(Filename, JsonConvert.SerializeObject(messages));
  139. return Task.FromResult(0);
  140. }
  141. public Task<IList<MqttApplicationMessage>> LoadRetainedMessagesAsync()
  142. {
  143. IList<MqttApplicationMessage> retainedMessages;
  144. if (File.Exists(Filename))
  145. {
  146. var json = File.ReadAllText(Filename);
  147. retainedMessages = JsonConvert.DeserializeObject<List<MqttApplicationMessage>>(json);
  148. }
  149. else
  150. {
  151. retainedMessages = new List<MqttApplicationMessage>();
  152. }
  153. return Task.FromResult(retainedMessages);
  154. }
  155. }
  156. }