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 3.7 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. [![Build Status](https://img.shields.io/nuget/v/MQTTnet.svg)](https://www.nuget.org/packages/MQTTnet/)
  5. # MQTTnet
  6. 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/.
  7. ## Features
  8. * MQTT client included
  9. * MQTT server (broker) included
  10. * TLS 1.2 support for client and server (but not UWP servers)
  11. * Async support
  12. * List of connected clients available (server only)
  13. * Extensible communication channels (i.e. In-Memory, TCP, TCP+SSL, WebSockets (not included in this project))
  14. * Access to internal trace messages
  15. * Extensible client credential validation (server only)
  16. * Unit tested (48+ tests)
  17. * Lightweight (only the low level implementation of MQTT, no overhead)
  18. ## Supported frameworks
  19. * .NET Standard 1.3+
  20. * .NET Core 1.1+
  21. * .NET Core App 1.1+
  22. * .NET Framework 4.5.2+ (x86, x64, AnyCPU)
  23. * Universal Windows (UWP) 10.0.10240+ (x86, x64, ARM, AnyCPU)
  24. ## Supported MQTT versions
  25. * 3.1.1
  26. ## Nuget
  27. This library is available as a nuget package: https://www.nuget.org/packages/MQTTnet/
  28. ## Contributions
  29. If you want to contribute to this project just create a pull request.
  30. ## References
  31. This library is used in the following projects:
  32. * HA4IoT (Open Source Home Automation system for .NET, https://github.com/chkr1011/HA4IoT)
  33. If you use this library and want to see your project here please let me know.
  34. # MqttClient
  35. ## Example
  36. ```csharp
  37. var options = new MqttClientOptions
  38. {
  39. Server = "localhost"
  40. };
  41. var client = new MqttClientFactory().CreateMqttClient(options);
  42. client.ApplicationMessageReceived += (s, e) =>
  43. {
  44. Console.WriteLine("### RECEIVED APPLICATION MESSAGE ###");
  45. Console.WriteLine($"+ Topic = {e.ApplicationMessage.Topic}");
  46. Console.WriteLine($"+ Payload = {Encoding.UTF8.GetString(e.ApplicationMessage.Payload)}");
  47. Console.WriteLine($"+ QoS = {e.ApplicationMessage.QualityOfServiceLevel}");
  48. Console.WriteLine($"+ Retain = {e.ApplicationMessage.Retain}");
  49. Console.WriteLine();
  50. };
  51. client.Connected += async (s, e) =>
  52. {
  53. Console.WriteLine("### CONNECTED WITH SERVER ###");
  54. await client.SubscribeAsync(new List<TopicFilter>
  55. {
  56. new TopicFilter("#", MqttQualityOfServiceLevel.AtMostOnce)
  57. });
  58. Console.WriteLine("### SUBSCRIBED ###");
  59. };
  60. client.Disconnected += async (s, e) =>
  61. {
  62. Console.WriteLine("### DISCONNECTED FROM SERVER ###");
  63. await Task.Delay(TimeSpan.FromSeconds(5));
  64. try
  65. {
  66. await client.ConnectAsync();
  67. }
  68. catch
  69. {
  70. Console.WriteLine("### RECONNECTING FAILED ###");
  71. }
  72. };
  73. try
  74. {
  75. await client.ConnectAsync();
  76. }
  77. catch
  78. {
  79. Console.WriteLine("### CONNECTING FAILED ###");
  80. }
  81. Console.WriteLine("### WAITING FOR APPLICATION MESSAGES ###");
  82. while (true)
  83. {
  84. Console.ReadLine();
  85. var applicationMessage = new MqttApplicationMessage(
  86. "A/B/C",
  87. Encoding.UTF8.GetBytes("Hello World"),
  88. MqttQualityOfServiceLevel.AtLeastOnce,
  89. false
  90. );
  91. await client.PublishAsync(applicationMessage);
  92. }
  93. ```
  94. # MqttServer
  95. ## Example
  96. ```csharp
  97. var options = new MqttServerOptions
  98. {
  99. ConnectionValidator = p =>
  100. {
  101. if (p.ClientId == "SpecialClient")
  102. {
  103. if (p.Username != "USER" || p.Password != "PASS")
  104. {
  105. return MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword;
  106. }
  107. }
  108. return MqttConnectReturnCode.ConnectionAccepted;
  109. }
  110. };
  111. var mqttServer = new MqttServerFactory().CreateMqttServer(options);
  112. mqttServer.Start();
  113. Console.WriteLine("Press any key to exit.");
  114. Console.ReadLine();
  115. mqttServer.Stop();
  116. ```