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.
 
 
 
 

180 lines
7.9 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 MQTTnet.Client;
  5. using System;
  6. using System.Security.Authentication;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using MQTTnet.Formatter;
  10. using MQTTnet.Implementations;
  11. using MQTTnet.Protocol;
  12. namespace MQTTnet.TestApp
  13. {
  14. public static class PublicBrokerTest
  15. {
  16. public static async Task RunAsync()
  17. {
  18. // MqttNetConsoleLogger.ForwardToConsole();
  19. // For most of these connections to work, set output target to Net5.0.
  20. #if NET5_0_OR_GREATER
  21. // TLS13 is only available in Net5.0
  22. var unsafeTls13 = new MqttClientOptionsBuilderTlsParameters
  23. {
  24. UseTls = true,
  25. SslProtocol = SslProtocols.Tls13,
  26. // Don't use this in production code. This handler simply allows any invalid certificate to work.
  27. CertificateValidationHandler = w => true
  28. };
  29. #endif
  30. // Also defining TLS12 for servers that don't seem no to support TLS13.
  31. var unsafeTls12 = new MqttClientOptionsBuilderTlsParameters
  32. {
  33. UseTls = true,
  34. SslProtocol = SslProtocols.Tls12,
  35. // Don't use this in production code. This handler simply allows any invalid certificate to work.
  36. CertificateValidationHandler = w => true
  37. };
  38. // mqtt.eclipseprojects.io
  39. await ExecuteTestAsync("mqtt.eclipseprojects.io TCP",
  40. new MqttClientOptionsBuilder().WithTcpServer("mqtt.eclipseprojects.io", 1883)
  41. .WithProtocolVersion(MqttProtocolVersion.V311).Build());
  42. await ExecuteTestAsync("mqtt.eclipseprojects.io WS",
  43. new MqttClientOptionsBuilder().WithWebSocketServer("mqtt.eclipseprojects.io:80/mqtt")
  44. .WithProtocolVersion(MqttProtocolVersion.V311).Build());
  45. #if NET5_0_OR_GREATER
  46. await ExecuteTestAsync("mqtt.eclipseprojects.io WS TLS13",
  47. new MqttClientOptionsBuilder().WithWebSocketServer("mqtt.eclipseprojects.io:443/mqtt")
  48. .WithProtocolVersion(MqttProtocolVersion.V311).WithTls(unsafeTls13).Build());
  49. #endif
  50. // test.mosquitto.org
  51. await ExecuteTestAsync("test.mosquitto.org TCP",
  52. new MqttClientOptionsBuilder().WithTcpServer("test.mosquitto.org", 1883)
  53. .WithProtocolVersion(MqttProtocolVersion.V311).Build());
  54. await ExecuteTestAsync("test.mosquitto.org TCP - Authenticated",
  55. new MqttClientOptionsBuilder().WithTcpServer("test.mosquitto.org", 1884)
  56. .WithCredentials("rw", "readwrite")
  57. .WithProtocolVersion(MqttProtocolVersion.V311).Build());
  58. await ExecuteTestAsync("test.mosquitto.org TCP TLS12",
  59. new MqttClientOptionsBuilder().WithTcpServer("test.mosquitto.org", 8883)
  60. .WithProtocolVersion(MqttProtocolVersion.V311).WithTls(unsafeTls12).Build());
  61. #if NET5_0_OR_GREATER
  62. await ExecuteTestAsync("test.mosquitto.org TCP TLS13",
  63. new MqttClientOptionsBuilder().WithTcpServer("test.mosquitto.org", 8883)
  64. .WithProtocolVersion(MqttProtocolVersion.V311).WithTls(unsafeTls13).Build());
  65. #endif
  66. await ExecuteTestAsync("test.mosquitto.org TCP TLS12 - Authenticated",
  67. new MqttClientOptionsBuilder().WithTcpServer("test.mosquitto.org", 8885)
  68. .WithCredentials("rw", "readwrite")
  69. .WithProtocolVersion(MqttProtocolVersion.V311).WithTls(unsafeTls12).Build());
  70. await ExecuteTestAsync("test.mosquitto.org WS",
  71. new MqttClientOptionsBuilder().WithWebSocketServer("test.mosquitto.org:8080/mqtt")
  72. .WithProtocolVersion(MqttProtocolVersion.V311).Build());
  73. await ExecuteTestAsync("test.mosquitto.org WS TLS12",
  74. new MqttClientOptionsBuilder().WithWebSocketServer("test.mosquitto.org:8081/mqtt")
  75. .WithProtocolVersion(MqttProtocolVersion.V311).WithTls(unsafeTls12).Build());
  76. // broker.emqx.io
  77. await ExecuteTestAsync("broker.emqx.io TCP",
  78. new MqttClientOptionsBuilder().WithTcpServer("broker.emqx.io", 1883)
  79. .WithProtocolVersion(MqttProtocolVersion.V311).Build());
  80. await ExecuteTestAsync("broker.emqx.io TCP TLS12",
  81. new MqttClientOptionsBuilder().WithTcpServer("broker.emqx.io", 8883)
  82. .WithProtocolVersion(MqttProtocolVersion.V311).WithTls(unsafeTls12).Build());
  83. #if NET5_0_OR_GREATER
  84. await ExecuteTestAsync("broker.emqx.io TCP TLS13",
  85. new MqttClientOptionsBuilder().WithTcpServer("broker.emqx.io", 8883)
  86. .WithProtocolVersion(MqttProtocolVersion.V311).WithTls(unsafeTls13).Build());
  87. #endif
  88. await ExecuteTestAsync("broker.emqx.io WS",
  89. new MqttClientOptionsBuilder().WithWebSocketServer("broker.emqx.io:8083/mqtt")
  90. .WithProtocolVersion(MqttProtocolVersion.V311).Build());
  91. await ExecuteTestAsync("broker.emqx.io WS TLS12",
  92. new MqttClientOptionsBuilder().WithWebSocketServer("broker.emqx.io:8084/mqtt")
  93. .WithProtocolVersion(MqttProtocolVersion.V311).WithTls(unsafeTls12).Build());
  94. // broker.hivemq.com
  95. await ExecuteTestAsync("broker.hivemq.com TCP",
  96. new MqttClientOptionsBuilder().WithTcpServer("broker.hivemq.com", 1883)
  97. .WithProtocolVersion(MqttProtocolVersion.V311).Build());
  98. await ExecuteTestAsync("broker.hivemq.com WS",
  99. new MqttClientOptionsBuilder().WithWebSocketServer("broker.hivemq.com:8000/mqtt")
  100. .WithProtocolVersion(MqttProtocolVersion.V311).Build());
  101. // mqtt.swifitch.cz: Does not seem to operate any more
  102. // cloudmqtt.com: Cannot test because it does not offer a free plan any more.
  103. Write("Finished.", ConsoleColor.White);
  104. Console.ReadLine();
  105. }
  106. static async Task ExecuteTestAsync(string name, MqttClientOptions options)
  107. {
  108. try
  109. {
  110. Write("Testing '" + name + "'... ", ConsoleColor.Gray);
  111. var factory = new MqttFactory();
  112. //factory.UseWebSocket4Net();
  113. var client = factory.CreateMqttClient();
  114. var topic = Guid.NewGuid().ToString();
  115. MqttApplicationMessage receivedMessage = null;
  116. client.ApplicationMessageReceivedAsync += e =>
  117. {
  118. receivedMessage = e.ApplicationMessage;
  119. return PlatformAbstractionLayer.CompletedTask;
  120. };
  121. await client.ConnectAsync(options);
  122. await client.SubscribeAsync(topic, MqttQualityOfServiceLevel.AtLeastOnce);
  123. await client.PublishStringAsync(topic, "Hello_World", MqttQualityOfServiceLevel.AtLeastOnce);
  124. SpinWait.SpinUntil(() => receivedMessage != null, 5000);
  125. if (receivedMessage?.Topic != topic || receivedMessage?.ConvertPayloadToString() != "Hello_World")
  126. {
  127. throw new Exception("Message invalid.");
  128. }
  129. await client.UnsubscribeAsync(topic);
  130. await client.DisconnectAsync();
  131. Write("[OK]\n", ConsoleColor.Green);
  132. }
  133. catch (Exception e)
  134. {
  135. Write("[FAILED] " + e.Message + "\n", ConsoleColor.Red);
  136. }
  137. }
  138. static void Write(string message, ConsoleColor color)
  139. {
  140. Console.ForegroundColor = color;
  141. Console.Write(message);
  142. }
  143. }
  144. }