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.
 
 
 
 

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