Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

README.md 4.3 KiB

há 7 anos
há 7 anos
há 7 anos
há 7 anos
há 7 anos
há 7 anos
há 7 anos
há 7 anos
há 7 anos
há 7 anos
há 7 anos
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. * Lightweight (only the low level implementation of MQTT, no overhead)
  14. * Performance optimized (processing ~25.000 messages / second)*
  15. * Interfaces included for mocking and testing
  16. * Access to internal trace messages
  17. * Unit tested (57+ tests)
  18. \* Tested on local machine with MQTTnet client and server running in the same process and using TCP channel.
  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, SUBSCRIBING ###");
  66. await client.SubscribeAsync(new List<TopicFilter>
  67. {
  68. new TopicFilter("#", MqttQualityOfServiceLevel.AtMostOnce)
  69. });
  70. };
  71. client.Disconnected += async (s, e) =>
  72. {
  73. Console.WriteLine("### DISCONNECTED FROM SERVER ###");
  74. await Task.Delay(TimeSpan.FromSeconds(5));
  75. try
  76. {
  77. await client.ConnectAsync();
  78. }
  79. catch
  80. {
  81. Console.WriteLine("### RECONNECTING FAILED ###");
  82. }
  83. };
  84. try
  85. {
  86. await client.ConnectAsync();
  87. }
  88. catch
  89. {
  90. Console.WriteLine("### CONNECTING FAILED ###");
  91. }
  92. Console.WriteLine("### WAITING FOR APPLICATION MESSAGES ###");
  93. var messageFactory = new MqttApplicationMessageFactory();
  94. while (true)
  95. {
  96. Console.ReadLine();
  97. var applicationMessage = messageFactory.CreateApplicationMessage("myTopic", "Hello World", MqttQualityOfServiceLevel.AtLeastOnce);
  98. await client.PublishAsync(applicationMessage);
  99. }
  100. ```
  101. ## MqttServer
  102. ```csharp
  103. var options = new MqttServerOptions
  104. {
  105. ConnectionValidator = p =>
  106. {
  107. if (p.ClientId == "SpecialClient")
  108. {
  109. if (p.Username != "USER" || p.Password != "PASS")
  110. {
  111. return MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword;
  112. }
  113. }
  114. return MqttConnectReturnCode.ConnectionAccepted;
  115. }
  116. };
  117. var mqttServer = new MqttServerFactory().CreateMqttServer(options);
  118. mqttServer.Start();
  119. Console.WriteLine("Press any key to exit.");
  120. Console.ReadLine();
  121. mqttServer.Stop();
  122. ```