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.
 
 
 
 

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