No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 

43 líneas
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. }