Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 

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