Add async handlers for connect and disconnect, too

.. this allows a client to properly async publish on connect.
This commit is contained in:
Dominik Psenner
2019-02-15 17:02:33 +01:00
parent 5e6b902430
commit 2000de15e7
3 changed files with 40 additions and 3 deletions
@@ -2,6 +2,8 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using MQTTnet.Client.Connecting;
using MQTTnet.Client.Disconnecting;
using MQTTnet.Client.Publishing;
using MQTTnet.Client.Receiving;
using MQTTnet.Protocol;
@@ -10,6 +12,30 @@ namespace MQTTnet.Extensions.ManagedClient
{
public static class ManagedMqttClientExtensions
{
public static IManagedMqttClient UseConnectedHandler(this IManagedMqttClient client, Func<MqttClientConnectedEventArgs, Task> handler)
{
if (handler == null)
{
client.ConnectedHandler = null;
return client;
}
client.ConnectedHandler = new MqttClientConnectedHandlerDelegate(handler);
return client;
}
public static IManagedMqttClient UseDisconnectedHandler(this IManagedMqttClient client, Func<MqttClientDisconnectedEventArgs, Task> handler)
{
if (handler == null)
{
client.DisconnectedHandler = null;
return client;
}
client.DisconnectedHandler = new MqttClientDisconnectedHandlerDelegate(handler);
return client;
}
public static IManagedMqttClient UseReceivedApplicationMessageHandler(this IManagedMqttClient client, Func<MqttApplicationMessageHandlerContext, Task> handler)
{
if (handler == null)