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.
 
 
 
 

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