You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 4.2 KiB

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