25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 4.0 KiB

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