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.

README.md 2.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <p align="center">
  2. <img src="https://github.com/chkr1011/MQTTnet/blob/master/Images/Logo_128x128.png?raw=true" width="128">
  3. </p>
  4. # MQTTnet
  5. MQTTnet is a .NET library for MQTT based communication. It provides a MQTT client and a MQTT server. The implementation is based on the documentation from http://mqtt.org/.
  6. ## Supported frameworks
  7. * .NET Standard 1.1+
  8. * .NET Core 1.1+
  9. * .NET Core App 1.1+
  10. * .NET Framework 4.5.2+ (x86, x64, AnyCPU)
  11. * Universal Windows (UWP) 10.0.10240+ (x86, x64, ARM, AnyCPU)
  12. ## Supported MQTT versions
  13. * 3.1.1
  14. ## Nuget
  15. This library is available as a nuget package: https://www.nuget.org/packages/MQTTnet/
  16. ## Contributions
  17. If you want to contribute to this project just create a pull request.
  18. # MqttClient
  19. ## Example
  20. ```c#
  21. var options = new MqttClientOptions
  22. {
  23. Server = "localhost"
  24. };
  25. var client = new MqttClientFactory().CreateMqttClient(options);
  26. client.ApplicationMessageReceived += (s, e) =>
  27. {
  28. Console.WriteLine("### RECEIVED APPLICATION MESSAGE ###");
  29. Console.WriteLine($"+ Topic = {e.ApplicationMessage.Topic}");
  30. Console.WriteLine($"+ Payload = {Encoding.UTF8.GetString(e.ApplicationMessage.Payload)}");
  31. Console.WriteLine($"+ QoS = {e.ApplicationMessage.QualityOfServiceLevel}");
  32. Console.WriteLine($"+ Retain = {e.ApplicationMessage.Retain}");
  33. Console.WriteLine();
  34. };
  35. client.Connected += async (s, e) =>
  36. {
  37. Console.WriteLine("### CONNECTED WITH SERVER ###");
  38. await client.SubscribeAsync(new List<TopicFilter>
  39. {
  40. new TopicFilter("#", MqttQualityOfServiceLevel.AtMostOnce)
  41. });
  42. Console.WriteLine("### SUBSCRIBED ###");
  43. };
  44. client.Disconnected += async (s, e) =>
  45. {
  46. Console.WriteLine("### DISCONNECTED FROM SERVER ###");
  47. await Task.Delay(TimeSpan.FromSeconds(5));
  48. try
  49. {
  50. await client.ConnectAsync();
  51. }
  52. catch
  53. {
  54. Console.WriteLine("### RECONNECTING FAILED ###");
  55. }
  56. };
  57. try
  58. {
  59. await client.ConnectAsync();
  60. }
  61. catch
  62. {
  63. Console.WriteLine("### CONNECTING FAILED ###");
  64. }
  65. Console.WriteLine("### WAITING FOR APPLICATION MESSAGES ###");
  66. while (true)
  67. {
  68. Console.ReadLine();
  69. var applicationMessage = new MqttApplicationMessage(
  70. "A/B/C",
  71. Encoding.UTF8.GetBytes("Hello World"),
  72. MqttQualityOfServiceLevel.AtLeastOnce,
  73. false
  74. );
  75. await client.PublishAsync(applicationMessage);
  76. }
  77. ```
  78. # MqttServer
  79. ## Example
  80. ```c#
  81. var options = new MqttServerOptions
  82. {
  83. ConnectionValidator = p =>
  84. {
  85. if (p.ClientId == "SpecialClient")
  86. {
  87. if (p.Username != "USER" || p.Password != "PASS")
  88. {
  89. return MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword;
  90. }
  91. }
  92. return MqttConnectReturnCode.ConnectionAccepted;
  93. }
  94. };
  95. var mqttServer = new MqttServerFactory().CreateMqttServer(options);
  96. mqttServer.Start();
  97. Console.WriteLine("Press any key to exit.");
  98. Console.ReadLine();
  99. mqttServer.Stop();
  100. ```