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 3.0 KiB

7 jaren geleden
7 jaren geleden
7 jaren geleden
7 jaren geleden
7 jaren geleden
7 jaren geleden
7 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. [![Build Status](https://img.shields.io/nuget/v/MQTTnet.svg)](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. The implementation is based on the documentation from http://mqtt.org/.
  7. ## Supported frameworks
  8. * .NET Standard 1.1+
  9. * .NET Core 1.1+
  10. * .NET Core App 1.1+
  11. * .NET Framework 4.5.2+ (x86, x64, AnyCPU)
  12. * Universal Windows (UWP) 10.0.10240+ (x86, x64, ARM, AnyCPU)
  13. ## Supported MQTT versions
  14. * 3.1.1
  15. ## Nuget
  16. This library is available as a nuget package: https://www.nuget.org/packages/MQTTnet/
  17. ## Contributions
  18. If you want to contribute to this project just create a pull request.
  19. # MqttClient
  20. ## Example
  21. ```c#
  22. var options = new MqttClientOptions
  23. {
  24. Server = "localhost"
  25. };
  26. var client = new MqttClientFactory().CreateMqttClient(options);
  27. client.ApplicationMessageReceived += (s, e) =>
  28. {
  29. Console.WriteLine("### RECEIVED APPLICATION MESSAGE ###");
  30. Console.WriteLine($"+ Topic = {e.ApplicationMessage.Topic}");
  31. Console.WriteLine($"+ Payload = {Encoding.UTF8.GetString(e.ApplicationMessage.Payload)}");
  32. Console.WriteLine($"+ QoS = {e.ApplicationMessage.QualityOfServiceLevel}");
  33. Console.WriteLine($"+ Retain = {e.ApplicationMessage.Retain}");
  34. Console.WriteLine();
  35. };
  36. client.Connected += async (s, e) =>
  37. {
  38. Console.WriteLine("### CONNECTED WITH SERVER ###");
  39. await client.SubscribeAsync(new List<TopicFilter>
  40. {
  41. new TopicFilter("#", MqttQualityOfServiceLevel.AtMostOnce)
  42. });
  43. Console.WriteLine("### SUBSCRIBED ###");
  44. };
  45. client.Disconnected += async (s, e) =>
  46. {
  47. Console.WriteLine("### DISCONNECTED FROM SERVER ###");
  48. await Task.Delay(TimeSpan.FromSeconds(5));
  49. try
  50. {
  51. await client.ConnectAsync();
  52. }
  53. catch
  54. {
  55. Console.WriteLine("### RECONNECTING FAILED ###");
  56. }
  57. };
  58. try
  59. {
  60. await client.ConnectAsync();
  61. }
  62. catch
  63. {
  64. Console.WriteLine("### CONNECTING FAILED ###");
  65. }
  66. Console.WriteLine("### WAITING FOR APPLICATION MESSAGES ###");
  67. while (true)
  68. {
  69. Console.ReadLine();
  70. var applicationMessage = new MqttApplicationMessage(
  71. "A/B/C",
  72. Encoding.UTF8.GetBytes("Hello World"),
  73. MqttQualityOfServiceLevel.AtLeastOnce,
  74. false
  75. );
  76. await client.PublishAsync(applicationMessage);
  77. }
  78. ```
  79. # MqttServer
  80. ## Example
  81. ```c#
  82. var options = new MqttServerOptions
  83. {
  84. ConnectionValidator = p =>
  85. {
  86. if (p.ClientId == "SpecialClient")
  87. {
  88. if (p.Username != "USER" || p.Password != "PASS")
  89. {
  90. return MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword;
  91. }
  92. }
  93. return MqttConnectReturnCode.ConnectionAccepted;
  94. }
  95. };
  96. var mqttServer = new MqttServerFactory().CreateMqttServer(options);
  97. mqttServer.Start();
  98. Console.WriteLine("Press any key to exit.");
  99. Console.ReadLine();
  100. mqttServer.Stop();
  101. ```