Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

MqttClientExtensions.cs 1.4 KiB

1234567891011121314151617181920212223242526272829303132333435
  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 UnsubscribeAsync(this IMqttClient client, params string[] topicFilters)
  23. {
  24. if (client == null) throw new ArgumentNullException(nameof(client));
  25. if (topicFilters == null) throw new ArgumentNullException(nameof(topicFilters));
  26. return client.UnsubscribeAsync(topicFilters.ToList());
  27. }
  28. }
  29. }