Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

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