Você não pode selecionar mais de 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.0 KiB

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