您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

130 行
5.1 KiB

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