Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

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