You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

199 lines
7.6 KiB

  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. using System.IO;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using MQTTnet.Diagnostics;
  11. using MQTTnet.Extensions.ManagedClient;
  12. using MQTTnet.Implementations;
  13. using MQTTnet.Protocol;
  14. using MQTTnet.Server;
  15. using Newtonsoft.Json;
  16. namespace MQTTnet.TestApp
  17. {
  18. public static class ServerTest
  19. {
  20. public static void RunEmptyServer()
  21. {
  22. var mqttServer = new MqttFactory().CreateMqttServer(new MqttServerOptions());
  23. mqttServer.StartAsync().GetAwaiter().GetResult();
  24. Console.WriteLine("Press any key to exit.");
  25. Console.ReadLine();
  26. }
  27. public static void RunEmptyServerWithLogging()
  28. {
  29. var logger = new MqttNetEventLogger();
  30. MqttNetConsoleLogger.ForwardToConsole(logger);
  31. var mqttFactory = new MqttFactory(logger);
  32. var mqttServer = mqttFactory.CreateMqttServer(new MqttServerOptions());
  33. mqttServer.StartAsync().GetAwaiter().GetResult();
  34. Console.WriteLine("Press any key to exit.");
  35. Console.ReadLine();
  36. }
  37. public static async Task RunAsync()
  38. {
  39. try
  40. {
  41. var options = new MqttServerOptions();
  42. // Extend the timestamp for all messages from clients.
  43. // Protect several topics from being subscribed from every client.
  44. //var certificate = new X509Certificate(@"C:\certs\test\test.cer", "");
  45. //options.TlsEndpointOptions.Certificate = certificate.Export(X509ContentType.Cert);
  46. //options.ConnectionBacklog = 5;
  47. //options.DefaultEndpointOptions.IsEnabled = true;
  48. //options.TlsEndpointOptions.IsEnabled = false;
  49. var mqttServer = new MqttFactory().CreateMqttServer(options);
  50. const string Filename = "C:\\MQTT\\RetainedMessages.json";
  51. mqttServer.RetainedMessageChangedAsync += e =>
  52. {
  53. var directory = Path.GetDirectoryName(Filename);
  54. if (!Directory.Exists(directory))
  55. {
  56. Directory.CreateDirectory(directory);
  57. }
  58. File.WriteAllText(Filename, JsonConvert.SerializeObject(e.StoredRetainedMessages));
  59. return Task.FromResult(0);
  60. };
  61. mqttServer.RetainedMessagesClearedAsync += e =>
  62. {
  63. File.Delete(Filename);
  64. return Task.FromResult(0);
  65. };
  66. mqttServer.LoadingRetainedMessageAsync += e =>
  67. {
  68. List<MqttApplicationMessage> retainedMessages;
  69. if (File.Exists(Filename))
  70. {
  71. var json = File.ReadAllText(Filename);
  72. retainedMessages = JsonConvert.DeserializeObject<List<MqttApplicationMessage>>(json);
  73. }
  74. else
  75. {
  76. retainedMessages = new List<MqttApplicationMessage>();
  77. }
  78. e.LoadedRetainedMessages = retainedMessages;
  79. return Task.FromResult(0);
  80. };
  81. mqttServer.InterceptingPublishAsync += e =>
  82. {
  83. if (MqttTopicFilterComparer.Compare(e.ApplicationMessage.Topic, "/myTopic/WithTimestamp/#") == MqttTopicFilterCompareResult.IsMatch)
  84. {
  85. // Replace the payload with the timestamp. But also extending a JSON
  86. // based payload with the timestamp is a suitable use case.
  87. e.ApplicationMessage.Payload = Encoding.UTF8.GetBytes(DateTime.Now.ToString("O"));
  88. }
  89. if (e.ApplicationMessage.Topic == "not_allowed_topic")
  90. {
  91. e.ProcessPublish = false;
  92. e.CloseConnection = true;
  93. }
  94. return PlatformAbstractionLayer.CompletedTask;
  95. };
  96. mqttServer.ValidatingConnectionAsync += e =>
  97. {
  98. if (e.ClientId == "SpecialClient")
  99. {
  100. if (e.Username != "USER" || e.Password != "PASS")
  101. {
  102. e.ReasonCode = MqttConnectReasonCode.BadUserNameOrPassword;
  103. }
  104. }
  105. return PlatformAbstractionLayer.CompletedTask;
  106. };
  107. mqttServer.InterceptingSubscriptionAsync += e =>
  108. {
  109. if (e.TopicFilter.Topic.StartsWith("admin/foo/bar") && e.ClientId != "theAdmin")
  110. {
  111. e.Response.ReasonCode = MqttSubscribeReasonCode.ImplementationSpecificError;
  112. }
  113. if (e.TopicFilter.Topic.StartsWith("the/secret/stuff") && e.ClientId != "Imperator")
  114. {
  115. e.Response.ReasonCode = MqttSubscribeReasonCode.ImplementationSpecificError;
  116. e.CloseConnection = true;
  117. }
  118. return PlatformAbstractionLayer.CompletedTask;
  119. };
  120. mqttServer.InterceptingPublishAsync += e =>
  121. {
  122. MqttNetConsoleLogger.PrintToConsole(
  123. $"'{e.ClientId}' reported '{e.ApplicationMessage.Topic}' > '{Encoding.UTF8.GetString(e.ApplicationMessage.Payload ?? new byte[0])}'",
  124. ConsoleColor.Magenta);
  125. return PlatformAbstractionLayer.CompletedTask;
  126. };
  127. //options.ApplicationMessageInterceptor = c =>
  128. //{
  129. // if (c.ApplicationMessage.Payload == null || c.ApplicationMessage.Payload.Length == 0)
  130. // {
  131. // return;
  132. // }
  133. // try
  134. // {
  135. // var content = JObject.Parse(Encoding.UTF8.GetString(c.ApplicationMessage.Payload));
  136. // var timestampProperty = content.Property("timestamp");
  137. // if (timestampProperty != null && timestampProperty.Value.Type == JTokenType.Null)
  138. // {
  139. // timestampProperty.Value = DateTime.Now.ToString("O");
  140. // c.ApplicationMessage.Payload = Encoding.UTF8.GetBytes(content.ToString());
  141. // }
  142. // }
  143. // catch (Exception)
  144. // {
  145. // }
  146. //};
  147. mqttServer.ClientConnectedAsync += e =>
  148. {
  149. Console.Write("Client disconnected event fired.");
  150. return PlatformAbstractionLayer.CompletedTask;
  151. };
  152. await mqttServer.StartAsync();
  153. Console.WriteLine("Press any key to exit.");
  154. Console.ReadLine();
  155. await mqttServer.StopAsync();
  156. }
  157. catch (Exception e)
  158. {
  159. Console.WriteLine(e);
  160. }
  161. Console.ReadLine();
  162. }
  163. }
  164. }