No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

README.md 4.3 KiB

hace 7 años
hace 7 años
hace 7 años
hace 7 años
hace 7 años
hace 7 años
hace 7 años
hace 7 años
hace 7 años
hace 7 años
hace 7 años
hace 7 años
hace 7 años
hace 7 años
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. [![NuGet Badge](https://buildstats.info/nuget/MQTTnet)](https://www.nuget.org/packages/MQTTnet)
  5. [![BCH compliance](https://bettercodehub.com/edge/badge/chkr1011/MQTTnet?branch=master)](https://bettercodehub.com/)
  6. # MQTTnet
  7. MQTTnet is a .NET library for MQTT based communication. It provides a MQTT client and a MQTT server (broker). The implementation is based on the documentation from http://mqtt.org/.
  8. # Features
  9. ### General
  10. * Async support
  11. * TLS 1.2 support for client and server (but not UWP servers)
  12. * Extensible communication channels (i.e. In-Memory, TCP, TCP+TLS, WS)
  13. * Performance optimized (processing ~20.000 messages / second)*
  14. * Lightweight (only the low level implementation of MQTT, no overhead)
  15. * Interfaces included for mocking and testing
  16. * Access to internal trace messages
  17. * Unit tested (55+ tests)
  18. \* Tested on local machine with MQTTnet client and server running in the same process.
  19. ### Client
  20. * Rx support (via another project)
  21. * Communication via TCP (+TLS) or WS (WebSocket)
  22. ### Server (broker)
  23. * List of connected clients available
  24. * Supports connected clients with different protocol versions at the same time
  25. * Able to publish its own messages (no loopback client required)
  26. * Able to receive every messages (no loopback client required)
  27. * Extensible client credential validation
  28. # Supported frameworks
  29. * .NET Standard 1.3+
  30. * .NET Core 1.1+
  31. * .NET Core App 1.1+
  32. * .NET Framework 4.5.2+ (x86, x64, AnyCPU)
  33. * Universal Windows (UWP) 10.0.10240+ (x86, x64, ARM, AnyCPU)
  34. # Supported MQTT versions
  35. * 3.1.1
  36. * 3.1.0
  37. # Nuget
  38. This library is available as a nuget package: https://www.nuget.org/packages/MQTTnet/
  39. # Contributions
  40. If you want to contribute to this project just create a pull request.
  41. # References
  42. This library is used in the following projects:
  43. * MQTT Client Rx (Wrapper for Reactive Extensions, https://github.com/1iveowl/MQTTClient.rx)
  44. * HA4IoT (Open Source Home Automation system for .NET, https://github.com/chkr1011/HA4IoT)
  45. If you use this library and want to see your project here please let me know.
  46. # Examples
  47. ## MqttClient
  48. ```csharp
  49. var options = new MqttClientOptions
  50. {
  51. Server = "localhost"
  52. };
  53. var client = new MqttClientFactory().CreateMqttClient(options);
  54. client.ApplicationMessageReceived += (s, e) =>
  55. {
  56. Console.WriteLine("### RECEIVED APPLICATION MESSAGE ###");
  57. Console.WriteLine($"+ Topic = {e.ApplicationMessage.Topic}");
  58. Console.WriteLine($"+ Payload = {Encoding.UTF8.GetString(e.ApplicationMessage.Payload)}");
  59. Console.WriteLine($"+ QoS = {e.ApplicationMessage.QualityOfServiceLevel}");
  60. Console.WriteLine($"+ Retain = {e.ApplicationMessage.Retain}");
  61. Console.WriteLine();
  62. };
  63. client.Connected += async (s, e) =>
  64. {
  65. Console.WriteLine("### CONNECTED WITH SERVER ###");
  66. await client.SubscribeAsync(new List<TopicFilter>
  67. {
  68. new TopicFilter("#", MqttQualityOfServiceLevel.AtMostOnce)
  69. });
  70. Console.WriteLine("### SUBSCRIBED ###");
  71. };
  72. client.Disconnected += async (s, e) =>
  73. {
  74. Console.WriteLine("### DISCONNECTED FROM SERVER ###");
  75. await Task.Delay(TimeSpan.FromSeconds(5));
  76. try
  77. {
  78. await client.ConnectAsync();
  79. }
  80. catch
  81. {
  82. Console.WriteLine("### RECONNECTING FAILED ###");
  83. }
  84. };
  85. try
  86. {
  87. await client.ConnectAsync();
  88. }
  89. catch
  90. {
  91. Console.WriteLine("### CONNECTING FAILED ###");
  92. }
  93. Console.WriteLine("### WAITING FOR APPLICATION MESSAGES ###");
  94. while (true)
  95. {
  96. Console.ReadLine();
  97. var applicationMessage = new MqttApplicationMessage(
  98. "A/B/C",
  99. Encoding.UTF8.GetBytes("Hello World"),
  100. MqttQualityOfServiceLevel.AtLeastOnce,
  101. false
  102. );
  103. await client.PublishAsync(applicationMessage);
  104. }
  105. ```
  106. ## MqttServer
  107. ```csharp
  108. var options = new MqttServerOptions
  109. {
  110. ConnectionValidator = p =>
  111. {
  112. if (p.ClientId == "SpecialClient")
  113. {
  114. if (p.Username != "USER" || p.Password != "PASS")
  115. {
  116. return MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword;
  117. }
  118. }
  119. return MqttConnectReturnCode.ConnectionAccepted;
  120. }
  121. };
  122. var mqttServer = new MqttServerFactory().CreateMqttServer(options);
  123. mqttServer.Start();
  124. Console.WriteLine("Press any key to exit.");
  125. Console.ReadLine();
  126. mqttServer.Stop();
  127. ```