您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ManagedMqttClientExtensions.cs 1.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839
  1. using System;
  2. using System.Threading.Tasks;
  3. using MQTTnet.Protocol;
  4. namespace MQTTnet.Extensions.ManagedClient
  5. {
  6. public static class ManagedMqttClientExtensions
  7. {
  8. public static Task SubscribeAsync(this IManagedMqttClient managedClient, params TopicFilter[] topicFilters)
  9. {
  10. if (managedClient == null) throw new ArgumentNullException(nameof(managedClient));
  11. return managedClient.SubscribeAsync(topicFilters);
  12. }
  13. public static Task SubscribeAsync(this IManagedMqttClient managedClient, string topic, MqttQualityOfServiceLevel qualityOfServiceLevel)
  14. {
  15. if (managedClient == null) throw new ArgumentNullException(nameof(managedClient));
  16. if (topic == null) throw new ArgumentNullException(nameof(topic));
  17. return managedClient.SubscribeAsync(new TopicFilterBuilder().WithTopic(topic).WithQualityOfServiceLevel(qualityOfServiceLevel).Build());
  18. }
  19. public static Task SubscribeAsync(this IManagedMqttClient managedClient, string topic)
  20. {
  21. if (managedClient == null) throw new ArgumentNullException(nameof(managedClient));
  22. if (topic == null) throw new ArgumentNullException(nameof(topic));
  23. return managedClient.SubscribeAsync(new TopicFilterBuilder().WithTopic(topic).Build());
  24. }
  25. public static Task UnsubscribeAsync(this IManagedMqttClient managedClient, params string[] topicFilters)
  26. {
  27. if (managedClient == null) throw new ArgumentNullException(nameof(managedClient));
  28. return managedClient.UnsubscribeAsync(topicFilters);
  29. }
  30. }
  31. }