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.
 
 
 
 

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