mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-15 00:15:15 +08:00
Add extension methods that provide a simpler api for IMqttClient and IManagedMqttClient connect/disconnect when the handlers do not need async/await
This commit is contained in:
@@ -24,6 +24,18 @@ namespace MQTTnet.Extensions.ManagedClient
|
||||
return client;
|
||||
}
|
||||
|
||||
public static IManagedMqttClient UseConnectedHandler(this IManagedMqttClient client, Action<MqttClientConnectedEventArgs> 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)
|
||||
@@ -36,6 +48,18 @@ namespace MQTTnet.Extensions.ManagedClient
|
||||
return client;
|
||||
}
|
||||
|
||||
public static IManagedMqttClient UseDisconnectedHandler(this IManagedMqttClient client, Action<MqttClientDisconnectedEventArgs> 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)
|
||||
|
||||
@@ -27,6 +27,18 @@ namespace MQTTnet.Client
|
||||
return client;
|
||||
}
|
||||
|
||||
public static IMqttClient UseConnectedHandler(this IMqttClient client, Action<MqttClientConnectedEventArgs> handler)
|
||||
{
|
||||
if (handler == null)
|
||||
{
|
||||
client.ConnectedHandler = null;
|
||||
return client;
|
||||
}
|
||||
|
||||
client.ConnectedHandler = new MqttClientConnectedHandlerDelegate(handler);
|
||||
return client;
|
||||
}
|
||||
|
||||
public static IMqttClient UseDisconnectedHandler(this IMqttClient client, Func<MqttClientDisconnectedEventArgs, Task> handler)
|
||||
{
|
||||
if (handler == null)
|
||||
@@ -39,6 +51,18 @@ namespace MQTTnet.Client
|
||||
return client;
|
||||
}
|
||||
|
||||
public static IMqttClient UseDisconnectedHandler(this IMqttClient client, Action<MqttClientDisconnectedEventArgs> handler)
|
||||
{
|
||||
if (handler == null)
|
||||
{
|
||||
client.DisconnectedHandler = null;
|
||||
return client;
|
||||
}
|
||||
|
||||
client.DisconnectedHandler = new MqttClientDisconnectedHandlerDelegate(handler);
|
||||
return client;
|
||||
}
|
||||
|
||||
public static IMqttClient UseReceivedApplicationMessageHandler(this IMqttClient client, Func<MqttApplicationMessageHandlerContext, Task> handler)
|
||||
{
|
||||
if (handler == null)
|
||||
|
||||
Reference in New Issue
Block a user