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