選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Program.cs 4.8 KiB

7年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. }