Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

132 řádky
5.8 KiB

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