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.

Program.cs 5.2 KiB

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