Refactor subscribe and unsubscribe to support MQTTv5.

This commit is contained in:
Christian Kratky
2019-01-21 21:38:39 +01:00
parent 0d6e244f84
commit ebf0e91ab8
39 changed files with 1183 additions and 476 deletions
+54 -2
View File
@@ -1,5 +1,6 @@
using System;
using System.Threading.Tasks;
using MQTTnet.Client.Receiving;
using MQTTnet.Client.Subscribing;
using MQTTnet.Client.Unsubscribing;
using MQTTnet.Protocol;
@@ -8,6 +9,51 @@ namespace MQTTnet.Client
{
public static class MqttClientExtensions
{
public static IMqttClient UseReceivedApplicationMessageHandler(this IMqttClient client, Func<MqttApplicationMessageHandlerContext, Task> handler)
{
if (handler == null)
{
client.ReceivedApplicationMessageHandler = null;
return client;
}
client.ReceivedApplicationMessageHandler = new MqttApplicationMessageHandlerDelegate(handler);
return client;
}
public static IMqttClient UseReceivedApplicationMessageHandler(this IMqttClient client, Action<MqttApplicationMessageHandlerContext> handler)
{
if (handler == null)
{
client.ReceivedApplicationMessageHandler = null;
return client;
}
client.ReceivedApplicationMessageHandler = new MqttApplicationMessageHandlerDelegate(handler);
return client;
}
public static IMqttClient UseReceivedApplicationMessageHandler(this IMqttClient client, IMqttApplicationMessageHandler handler)
{
client.ReceivedApplicationMessageHandler = handler;
return client;
}
public static Task ReconnectAsync(this IMqttClient client)
{
if (client == null) throw new ArgumentNullException(nameof(client));
if (client.Options == null)
{
throw new InvalidOperationException("_ReconnectAsync_ can be used only if _ConnectAsync_ was called before.");
}
return client.ConnectAsync(client.Options);
}
public static Task DisconnectAsync(this IMqttClient client)
{
if (client == null) throw new ArgumentNullException(nameof(client));
@@ -20,7 +66,10 @@ namespace MQTTnet.Client
if (client == null) throw new ArgumentNullException(nameof(client));
if (topicFilters == null) throw new ArgumentNullException(nameof(topicFilters));
return client.SubscribeAsync(topicFilters);
var options = new MqttClientSubscribeOptions();
options.TopicFilters.AddRange(topicFilters);
return client.SubscribeAsync(options);
}
public static Task<MqttClientSubscribeResult> SubscribeAsync(this IMqttClient client, string topic, MqttQualityOfServiceLevel qualityOfServiceLevel)
@@ -44,7 +93,10 @@ namespace MQTTnet.Client
if (client == null) throw new ArgumentNullException(nameof(client));
if (topicFilters == null) throw new ArgumentNullException(nameof(topicFilters));
return client.UnsubscribeAsync(topicFilters);
var options = new MqttClientUnsubscribeOptions();
options.TopicFilters.AddRange(topicFilters);
return client.UnsubscribeAsync(options);
}
}
}