Server_Simple_Samples.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. using MQTTnet;
  2. using MQTTnet.Diagnostics;
  3. using MQTTnet.Protocol;
  4. using MQTTnet.Server;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Diagnostics;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Text.Json;
  12. using System.Threading.Tasks;
  13. namespace MqttServerConsoleApp;
  14. public static class Server_Simple_Samples
  15. {
  16. public static async Task Force_Disconnecting_Client()
  17. {
  18. /*
  19. * This sample will disconnect a client.
  20. *
  21. * See _Run_Minimal_Server_ for more information.
  22. */
  23. using (var mqttServer = await StartMqttServer())
  24. {
  25. // Let the client connect.
  26. await Task.Delay(TimeSpan.FromSeconds(5));
  27. // Now disconnect the client (if connected).
  28. var affectedClient = (await mqttServer.GetClientsAsync()).FirstOrDefault(c => c.Id == "MyClient");
  29. if (affectedClient != null)
  30. {
  31. await affectedClient.DisconnectAsync();
  32. }
  33. }
  34. }
  35. public static async Task Publish_Message_From_Broker()
  36. {
  37. /*
  38. * This sample will publish a message directly at the broker.
  39. *
  40. * See _Run_Minimal_Server_ for more information.
  41. */
  42. using (var mqttServer = await StartMqttServer())
  43. {
  44. // Create a new message using the builder as usual.
  45. var message = new MqttApplicationMessageBuilder().WithTopic("HelloWorld").WithPayload("Test").Build();
  46. // Now inject the new message at the broker.
  47. await mqttServer.InjectApplicationMessage(
  48. new InjectedMqttApplicationMessage(message)
  49. {
  50. SenderClientId = "SenderClientId"
  51. });
  52. }
  53. }
  54. public static async Task Run_Minimal_Server()
  55. {
  56. /*
  57. * This sample starts a simple MQTT server which will accept any TCP connection.
  58. */
  59. var mqttFactory = new MqttFactory();
  60. // The port for the default endpoint is 1883.
  61. // The default endpoint is NOT encrypted!
  62. // Use the builder classes where possible.
  63. var mqttServerOptions = new MqttServerOptionsBuilder().WithDefaultEndpoint().Build();
  64. // The port can be changed using the following API (not used in this example).
  65. // new MqttServerOptionsBuilder()
  66. // .WithDefaultEndpoint()
  67. // .WithDefaultEndpointPort(1234)
  68. // .Build();
  69. using (var mqttServer = mqttFactory.CreateMqttServer(mqttServerOptions))
  70. {
  71. await mqttServer.StartAsync();
  72. mqttServer.SubscribeAsync("ug67", "uplink");
  73. mqttServer.SubscribeAsync("ug67", "downlink/$deveui");
  74. var message = new MqttApplicationMessageBuilder().WithTopic("downlink/$deveui").WithPayload("Test").Build();
  75. // Now inject the new message at the broker.
  76. await mqttServer.InjectApplicationMessage(
  77. new InjectedMqttApplicationMessage(message)
  78. {
  79. SenderClientId = "SenderClientId"
  80. });
  81. mqttServer.InterceptingPublishAsync += MqttServer_InterceptingPublishAsync;
  82. Console.WriteLine("Press Enter to exit.");
  83. Console.ReadLine();
  84. // Stop and dispose the MQTT server if it is no longer needed!
  85. await mqttServer.StopAsync();
  86. }
  87. }
  88. private static Task MqttServer_InterceptingPublishAsync(InterceptingPublishEventArgs arg)
  89. {
  90. var str = Encoding.UTF8.GetString(arg.ApplicationMessage.Payload);
  91. Debug.WriteLine(str);
  92. return Task.CompletedTask;
  93. }
  94. public static async Task Run_Server_With_Logging()
  95. {
  96. /*
  97. * This sample starts a simple MQTT server and prints the logs to the output.
  98. *
  99. * IMPORTANT! Do not enable logging in live environment. It will decrease performance.
  100. *
  101. * See sample "Run_Minimal_Server" for more details.
  102. */
  103. var mqttFactory = new MqttFactory(new ConsoleLogger());
  104. var mqttServerOptions = new MqttServerOptionsBuilder().WithDefaultEndpoint().Build();
  105. using (var mqttServer = mqttFactory.CreateMqttServer(mqttServerOptions))
  106. {
  107. await mqttServer.StartAsync();
  108. Console.WriteLine("Press Enter to exit.");
  109. //mqttServer.
  110. Console.ReadLine();
  111. // Stop and dispose the MQTT server if it is no longer needed!
  112. await mqttServer.StopAsync();
  113. }
  114. }
  115. public static async Task Validating_Connections()
  116. {
  117. /*
  118. * This sample starts a simple MQTT server which will check for valid credentials and client ID.
  119. *
  120. * See _Run_Minimal_Server_ for more information.
  121. */
  122. var mqttFactory = new MqttFactory();
  123. var mqttServerOptions = new MqttServerOptionsBuilder().WithDefaultEndpoint().Build();
  124. using (var mqttServer = mqttFactory.CreateMqttServer(mqttServerOptions))
  125. {
  126. // Setup connection validation before starting the server so that there is
  127. // no change to connect without valid credentials.
  128. mqttServer.ValidatingConnectionAsync += e =>
  129. {
  130. if (e.ClientId != "ValidClientId")
  131. {
  132. e.ReasonCode = MqttConnectReasonCode.ClientIdentifierNotValid;
  133. }
  134. if (e.Username != "ValidUser")
  135. {
  136. e.ReasonCode = MqttConnectReasonCode.BadUserNameOrPassword;
  137. }
  138. if (e.Password != "SecretPassword")
  139. {
  140. e.ReasonCode = MqttConnectReasonCode.BadUserNameOrPassword;
  141. }
  142. return Task.CompletedTask;
  143. };
  144. await mqttServer.StartAsync();
  145. Console.WriteLine("Press Enter to exit.");
  146. Console.ReadLine();
  147. await mqttServer.StopAsync();
  148. }
  149. }
  150. static async Task<MqttServer> StartMqttServer()
  151. {
  152. var mqttFactory = new MqttFactory();
  153. // Due to security reasons the "default" endpoint (which is unencrypted) is not enabled by default!
  154. var mqttServerOptions = mqttFactory.CreateServerOptionsBuilder().WithDefaultEndpoint().Build();
  155. var server = mqttFactory.CreateMqttServer(mqttServerOptions);
  156. await server.StartAsync();
  157. return server;
  158. }
  159. public static async Task Persist_Retained_Messages()
  160. {
  161. /*
  162. * This sample starts a simple MQTT server which will store all retained messages in a file.
  163. */
  164. var storePath = "./RetainedMessages.json";
  165. var mqttFactory = new MqttFactory();
  166. //defautport 1883
  167. // Due to security reasons the "default" endpoint (which is unencrypted) is not enabled by default!
  168. var mqttServerOptions = mqttFactory.CreateServerOptionsBuilder().WithDefaultEndpoint().Build();
  169. using (var server = mqttFactory.CreateMqttServer(mqttServerOptions))
  170. {
  171. // Make sure that the server will load the retained messages.
  172. server.LoadingRetainedMessageAsync += async eventArgs =>
  173. {
  174. try
  175. {
  176. eventArgs.LoadedRetainedMessages = await JsonSerializer.DeserializeAsync<List<MqttApplicationMessage>>(File.OpenRead(storePath));
  177. Console.WriteLine("Retained messages loaded.");
  178. }
  179. catch (FileNotFoundException)
  180. {
  181. // Ignore because nothing is stored yet.
  182. Console.WriteLine("No retained messages stored yet.");
  183. }
  184. catch (Exception exception)
  185. {
  186. Console.WriteLine(exception);
  187. }
  188. };
  189. // Make sure to persist the changed retained messages.
  190. server.RetainedMessageChangedAsync += async eventArgs =>
  191. {
  192. try
  193. {
  194. // This sample uses the property _StoredRetainedMessages_ which will contain all(!) retained messages.
  195. // The event args also contain the affected retained message (property ChangedRetainedMessage). This can be
  196. // used to write all retained messages to dedicated files etc. Then all files must be loaded and a full list
  197. // of retained messages must be provided in the loaded event.
  198. var buffer = JsonSerializer.SerializeToUtf8Bytes(eventArgs.StoredRetainedMessages);
  199. await File.WriteAllBytesAsync(storePath, buffer);
  200. Console.WriteLine("Retained messages saved.");
  201. }
  202. catch (Exception exception)
  203. {
  204. Console.WriteLine(exception);
  205. }
  206. };
  207. // Make sure to clear the retained messages when they are all deleted via API.
  208. server.RetainedMessagesClearedAsync += _ =>
  209. {
  210. File.Delete(storePath);
  211. return Task.CompletedTask;
  212. };
  213. await server.StartAsync();
  214. server.SubscribeAsync("ug67","ceshi");
  215. Console.WriteLine("Press Enter to exit.");
  216. Console.ReadLine();
  217. }
  218. }
  219. class ConsoleLogger : IMqttNetLogger
  220. {
  221. readonly object _consoleSyncRoot = new();
  222. public bool IsEnabled => true;
  223. public void Publish(MqttNetLogLevel logLevel, string source, string message, object[]? parameters, Exception? exception)
  224. {
  225. var foregroundColor = ConsoleColor.White;
  226. switch (logLevel)
  227. {
  228. case MqttNetLogLevel.Verbose:
  229. foregroundColor = ConsoleColor.White;
  230. break;
  231. case MqttNetLogLevel.Info:
  232. foregroundColor = ConsoleColor.Green;
  233. break;
  234. case MqttNetLogLevel.Warning:
  235. foregroundColor = ConsoleColor.DarkYellow;
  236. break;
  237. case MqttNetLogLevel.Error:
  238. foregroundColor = ConsoleColor.Red;
  239. break;
  240. }
  241. if (parameters?.Length > 0)
  242. {
  243. message = string.Format(message, parameters);
  244. }
  245. lock (_consoleSyncRoot)
  246. {
  247. Console.ForegroundColor = foregroundColor;
  248. Console.WriteLine(message);
  249. if (exception != null)
  250. {
  251. Console.WriteLine(exception);
  252. }
  253. }
  254. }
  255. }
  256. }