Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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