|
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using MQTTnet.Protocol;
-
- namespace MQTTnet.Client
- {
- public static class MqttClientExtensions
- {
- public static Task<IList<MqttSubscribeResult>> SubscribeAsync(this IMqttClient client, params TopicFilter[] topicFilters)
- {
- if (client == null) throw new ArgumentNullException(nameof(client));
- if (topicFilters == null) throw new ArgumentNullException(nameof(topicFilters));
-
- return client.SubscribeAsync(topicFilters.ToList());
- }
-
- public static Task<IList<MqttSubscribeResult>> SubscribeAsync(this IMqttClient client, string topic, MqttQualityOfServiceLevel qualityOfServiceLevel)
- {
- if (client == null) throw new ArgumentNullException(nameof(client));
- if (topic == null) throw new ArgumentNullException(nameof(topic));
-
- return client.SubscribeAsync(new TopicFilterBuilder().WithTopic(topic).WithQualityOfServiceLevel(qualityOfServiceLevel).Build());
- }
-
- public static Task<IList<MqttSubscribeResult>> SubscribeAsync(this IMqttClient client, string topic)
- {
- if (client == null) throw new ArgumentNullException(nameof(client));
- if (topic == null) throw new ArgumentNullException(nameof(topic));
-
- return client.SubscribeAsync(new TopicFilterBuilder().WithTopic(topic).Build());
- }
-
- public static Task UnsubscribeAsync(this IMqttClient client, params string[] topicFilters)
- {
- if (client == null) throw new ArgumentNullException(nameof(client));
- if (topicFilters == null) throw new ArgumentNullException(nameof(topicFilters));
-
- return client.UnsubscribeAsync(topicFilters.ToList());
- }
- }
- }
|