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

43 lines
1.8 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using MQTTnet.Protocol;
  6. namespace MQTTnet.Client
  7. {
  8. public static class MqttClientExtensions
  9. {
  10. public static Task<IList<MqttSubscribeResult>> SubscribeAsync(this IMqttClient client, params TopicFilter[] topicFilters)
  11. {
  12. if (client == null) throw new ArgumentNullException(nameof(client));
  13. if (topicFilters == null) throw new ArgumentNullException(nameof(topicFilters));
  14. return client.SubscribeAsync(topicFilters.ToList());
  15. }
  16. public static Task<IList<MqttSubscribeResult>> SubscribeAsync(this IMqttClient client, string topic, MqttQualityOfServiceLevel qualityOfServiceLevel)
  17. {
  18. if (client == null) throw new ArgumentNullException(nameof(client));
  19. if (topic == null) throw new ArgumentNullException(nameof(topic));
  20. return client.SubscribeAsync(new TopicFilterBuilder().WithTopic(topic).WithQualityOfServiceLevel(qualityOfServiceLevel).Build());
  21. }
  22. public static Task<IList<MqttSubscribeResult>> SubscribeAsync(this IMqttClient client, string topic)
  23. {
  24. if (client == null) throw new ArgumentNullException(nameof(client));
  25. if (topic == null) throw new ArgumentNullException(nameof(topic));
  26. return client.SubscribeAsync(new TopicFilterBuilder().WithTopic(topic).Build());
  27. }
  28. public static Task UnsubscribeAsync(this IMqttClient client, params string[] topicFilters)
  29. {
  30. if (client == null) throw new ArgumentNullException(nameof(client));
  31. if (topicFilters == null) throw new ArgumentNullException(nameof(topicFilters));
  32. return client.UnsubscribeAsync(topicFilters.ToList());
  33. }
  34. }
  35. }