Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

136 rader
5.9 KiB

  1. using MQTTnet.Client;
  2. using System;
  3. using System.IO;
  4. using System.Net;
  5. using System.Security.Authentication;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using MQTTnet.Client.Options;
  9. using MQTTnet.Client.Receiving;
  10. using MQTTnet.Extensions.WebSocket4Net;
  11. using MQTTnet.Formatter;
  12. using MQTTnet.Protocol;
  13. using Newtonsoft.Json;
  14. namespace MQTTnet.TestApp.NetCore
  15. {
  16. public static class PublicBrokerTest
  17. {
  18. public static async Task RunAsync()
  19. {
  20. //MqttNetConsoleLogger.ForwardToConsole();
  21. // iot.eclipse.org
  22. await ExecuteTestAsync("iot.eclipse.org TCP",
  23. new MqttClientOptionsBuilder().WithTcpServer("iot.eclipse.org", 1883).WithProtocolVersion(MqttProtocolVersion.V311).Build());
  24. await ExecuteTestAsync("iot.eclipse.org WS",
  25. new MqttClientOptionsBuilder().WithWebSocketServer("iot.eclipse.org:80/mqtt").WithProtocolVersion(MqttProtocolVersion.V311).Build());
  26. await ExecuteTestAsync("iot.eclipse.org WS TLS",
  27. new MqttClientOptionsBuilder().WithWebSocketServer("iot.eclipse.org:443/mqtt").WithProtocolVersion(MqttProtocolVersion.V311).WithTls().Build());
  28. // test.mosquitto.org
  29. await ExecuteTestAsync("test.mosquitto.org TCP",
  30. new MqttClientOptionsBuilder().WithTcpServer("test.mosquitto.org", 1883).WithProtocolVersion(MqttProtocolVersion.V311).Build());
  31. await ExecuteTestAsync("test.mosquitto.org TCP TLS",
  32. new MqttClientOptionsBuilder().WithTcpServer("test.mosquitto.org", 8883).WithProtocolVersion(MqttProtocolVersion.V311).WithTls().Build());
  33. await ExecuteTestAsync("test.mosquitto.org WS",
  34. new MqttClientOptionsBuilder().WithWebSocketServer("test.mosquitto.org:8080/mqtt").WithProtocolVersion(MqttProtocolVersion.V311).Build());
  35. await ExecuteTestAsync("test.mosquitto.org WS TLS",
  36. new MqttClientOptionsBuilder().WithWebSocketServer("test.mosquitto.org:8081/mqtt").WithProtocolVersion(MqttProtocolVersion.V311).WithTls().Build());
  37. // broker.hivemq.com
  38. await ExecuteTestAsync("broker.hivemq.com TCP",
  39. new MqttClientOptionsBuilder().WithTcpServer("broker.hivemq.com", 1883).WithProtocolVersion(MqttProtocolVersion.V311).Build());
  40. await ExecuteTestAsync("broker.hivemq.com WS",
  41. new MqttClientOptionsBuilder().WithWebSocketServer("broker.hivemq.com:8000/mqtt").WithProtocolVersion(MqttProtocolVersion.V311).Build());
  42. // mqtt.swifitch.cz
  43. await ExecuteTestAsync("mqtt.swifitch.cz",
  44. new MqttClientOptionsBuilder().WithTcpServer("mqtt.swifitch.cz", 1883).WithProtocolVersion(MqttProtocolVersion.V311).Build());
  45. // CloudMQTT
  46. var configFile = Path.Combine("E:\\CloudMqttTestConfig.json");
  47. if (File.Exists(configFile))
  48. {
  49. var config = JsonConvert.DeserializeObject<MqttConfig>(File.ReadAllText(configFile));
  50. await ExecuteTestAsync("CloudMQTT TCP",
  51. new MqttClientOptionsBuilder().WithTcpServer(config.Server, config.Port).WithCredentials(config.Username, config.Password).WithProtocolVersion(MqttProtocolVersion.V311).Build());
  52. await ExecuteTestAsync("CloudMQTT TCP TLS",
  53. new MqttClientOptionsBuilder().WithTcpServer(config.Server, config.SslPort).WithCredentials(config.Username, config.Password).WithTls().WithProtocolVersion(MqttProtocolVersion.V311).Build());
  54. await ExecuteTestAsync("CloudMQTT WS TLS",
  55. new MqttClientOptionsBuilder().WithWebSocketServer(config.Server + ":" + config.SslWebSocketPort + "/mqtt").WithCredentials(config.Username, config.Password).WithTls().WithProtocolVersion(MqttProtocolVersion.V311).Build());
  56. }
  57. Write("Finished.", ConsoleColor.White);
  58. Console.ReadLine();
  59. }
  60. private static async Task ExecuteTestAsync(string name, IMqttClientOptions options)
  61. {
  62. try
  63. {
  64. Write("Testing '" + name + "'... ", ConsoleColor.Gray);
  65. var factory = new MqttFactory();
  66. //factory.UseWebSocket4Net();
  67. var client = factory.CreateMqttClient();
  68. var topic = Guid.NewGuid().ToString();
  69. MqttApplicationMessage receivedMessage = null;
  70. client.ApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(e => receivedMessage = e.ApplicationMessage);
  71. await client.ConnectAsync(options);
  72. await client.SubscribeAsync(topic, MqttQualityOfServiceLevel.AtLeastOnce);
  73. await client.PublishAsync(topic, "Hello_World", MqttQualityOfServiceLevel.AtLeastOnce);
  74. SpinWait.SpinUntil(() => receivedMessage != null, 5000);
  75. if (receivedMessage?.Topic != topic || receivedMessage?.ConvertPayloadToString() != "Hello_World")
  76. {
  77. throw new Exception("Message invalid.");
  78. }
  79. await client.UnsubscribeAsync(topic);
  80. await client.DisconnectAsync();
  81. Write("[OK]\n", ConsoleColor.Green);
  82. }
  83. catch (Exception e)
  84. {
  85. Write("[FAILED] " + e.Message + "\n", ConsoleColor.Red);
  86. }
  87. }
  88. private static void Write(string message, ConsoleColor color)
  89. {
  90. Console.ForegroundColor = color;
  91. Console.Write(message);
  92. }
  93. public class MqttConfig
  94. {
  95. public string Server { get; set; }
  96. public string Username { get; set; }
  97. public string Password { get; set; }
  98. public int Port { get; set; }
  99. public int SslPort { get; set; }
  100. public int WebSocketPort { get; set; }
  101. public int SslWebSocketPort { get; set; }
  102. }
  103. }
  104. }