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

Program.cs 5.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using MQTTnet.Core;
  7. using MQTTnet.Core.Client;
  8. using MQTTnet.Core.Diagnostics;
  9. using MQTTnet.Core.Packets;
  10. using MQTTnet.Core.Protocol;
  11. using MQTTnet.Core.Server;
  12. namespace MQTTnet.TestApp.NetFramework
  13. {
  14. public static class Program
  15. {
  16. public static void Main(string[] args)
  17. {
  18. Console.WriteLine("MQTTnet - TestApp.NetFramework");
  19. Console.WriteLine("1 = Start client");
  20. Console.WriteLine("2 = Start server");
  21. Console.WriteLine("3 = Start performance test");
  22. var pressedKey = Console.ReadKey(true);
  23. if (pressedKey.Key == ConsoleKey.D1)
  24. {
  25. Task.Run(() => RunClientAsync(args));
  26. Thread.Sleep(Timeout.Infinite);
  27. }
  28. else if (pressedKey.Key == ConsoleKey.D2)
  29. {
  30. Task.Run(() => RunServerAsync(args));
  31. Thread.Sleep(Timeout.Infinite);
  32. }
  33. else if (pressedKey.Key == ConsoleKey.D3)
  34. {
  35. Task.Run(PerformanceTest.RunAsync);
  36. Thread.Sleep(Timeout.Infinite);
  37. }
  38. }
  39. private static async Task RunClientAsync(string[] arguments)
  40. {
  41. MqttTrace.TraceMessagePublished += (s, e) =>
  42. {
  43. Console.WriteLine($">> [{e.ThreadId}] [{e.Source}] [{e.Level}]: {e.Message}");
  44. if (e.Exception != null)
  45. {
  46. Console.WriteLine(e.Exception);
  47. }
  48. };
  49. try
  50. {
  51. var options = new MqttClientOptions
  52. {
  53. Server = "localhost",
  54. ClientId = "XYZ",
  55. CleanSession = true
  56. };
  57. var client = new MqttClientFactory().CreateMqttClient(options);
  58. client.ApplicationMessageReceived += (s, e) =>
  59. {
  60. Console.WriteLine("### RECEIVED APPLICATION MESSAGE ###");
  61. Console.WriteLine($"+ Topic = {e.ApplicationMessage.Topic}");
  62. Console.WriteLine($"+ Payload = {Encoding.UTF8.GetString(e.ApplicationMessage.Payload)}");
  63. Console.WriteLine($"+ QoS = {e.ApplicationMessage.QualityOfServiceLevel}");
  64. Console.WriteLine($"+ Retain = {e.ApplicationMessage.Retain}");
  65. Console.WriteLine();
  66. };
  67. client.Connected += async (s, e) =>
  68. {
  69. Console.WriteLine("### CONNECTED WITH SERVER ###");
  70. await client.SubscribeAsync(new List<TopicFilter>
  71. {
  72. new TopicFilter("#", MqttQualityOfServiceLevel.AtMostOnce)
  73. });
  74. Console.WriteLine("### SUBSCRIBED ###");
  75. };
  76. client.Disconnected += async (s, e) =>
  77. {
  78. Console.WriteLine("### DISCONNECTED FROM SERVER ###");
  79. await Task.Delay(TimeSpan.FromSeconds(5));
  80. try
  81. {
  82. await client.ConnectAsync();
  83. }
  84. catch
  85. {
  86. Console.WriteLine("### RECONNECTING FAILED ###");
  87. }
  88. };
  89. try
  90. {
  91. await client.ConnectAsync();
  92. }
  93. catch (Exception exception)
  94. {
  95. Console.WriteLine("### CONNECTING FAILED ###" + Environment.NewLine + exception);
  96. }
  97. Console.WriteLine("### WAITING FOR APPLICATION MESSAGES ###");
  98. while (true)
  99. {
  100. Console.ReadLine();
  101. var applicationMessage = new MqttApplicationMessage(
  102. "A/B/C",
  103. Encoding.UTF8.GetBytes("Hello World"),
  104. MqttQualityOfServiceLevel.AtLeastOnce,
  105. false
  106. );
  107. await client.PublishAsync(applicationMessage);
  108. }
  109. }
  110. catch (Exception exception)
  111. {
  112. Console.WriteLine(exception);
  113. }
  114. }
  115. private static void RunServerAsync(string[] arguments)
  116. {
  117. MqttTrace.TraceMessagePublished += (s, e) =>
  118. {
  119. Console.WriteLine($">> [{e.ThreadId}] [{e.Source}] [{e.Level}]: {e.Message}");
  120. if (e.Exception != null)
  121. {
  122. Console.WriteLine(e.Exception);
  123. }
  124. };
  125. try
  126. {
  127. var options = new MqttServerOptions
  128. {
  129. ConnectionValidator = p =>
  130. {
  131. if (p.ClientId == "SpecialClient")
  132. {
  133. if (p.Username != "USER" || p.Password != "PASS")
  134. {
  135. return MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword;
  136. }
  137. }
  138. return MqttConnectReturnCode.ConnectionAccepted;
  139. }
  140. };
  141. var mqttServer = new MqttServerFactory().CreateMqttServer(options);
  142. mqttServer.Start();
  143. Console.WriteLine("Press any key to exit.");
  144. Console.ReadLine();
  145. mqttServer.Stop();
  146. }
  147. catch (Exception e)
  148. {
  149. Console.WriteLine(e);
  150. }
  151. Console.ReadLine();
  152. }
  153. }
  154. }