Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

MqttClientExtensions.cs 1.8 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. }