mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-21 12:16:02 +08:00
Add event for processed application message (managed client only)
This commit is contained in:
@@ -0,0 +1,19 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace MQTTnet.ManagedClient
|
||||||
|
{
|
||||||
|
public class ApplicationMessageProcessedEventArgs : EventArgs
|
||||||
|
{
|
||||||
|
public ApplicationMessageProcessedEventArgs(MqttApplicationMessage applicationMessage, Exception exception)
|
||||||
|
{
|
||||||
|
ApplicationMessage = applicationMessage ?? throw new ArgumentNullException(nameof(applicationMessage));
|
||||||
|
Exception = exception;
|
||||||
|
}
|
||||||
|
|
||||||
|
public MqttApplicationMessage ApplicationMessage { get; }
|
||||||
|
public Exception Exception { get; }
|
||||||
|
|
||||||
|
public bool HasFailed => Exception != null;
|
||||||
|
public bool HasSucceeded => Exception == null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -12,6 +12,8 @@ namespace MQTTnet.ManagedClient
|
|||||||
event EventHandler<MqttClientConnectedEventArgs> Connected;
|
event EventHandler<MqttClientConnectedEventArgs> Connected;
|
||||||
event EventHandler<MqttClientDisconnectedEventArgs> Disconnected;
|
event EventHandler<MqttClientDisconnectedEventArgs> Disconnected;
|
||||||
|
|
||||||
|
event EventHandler<ApplicationMessageProcessedEventArgs> ApplicationMessageProcessed;
|
||||||
|
|
||||||
Task StartAsync(IManagedMqttClientOptions options);
|
Task StartAsync(IManagedMqttClientOptions options);
|
||||||
Task StopAsync();
|
Task StopAsync();
|
||||||
|
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ namespace MQTTnet.ManagedClient
|
|||||||
public event EventHandler<MqttClientConnectedEventArgs> Connected;
|
public event EventHandler<MqttClientConnectedEventArgs> Connected;
|
||||||
public event EventHandler<MqttClientDisconnectedEventArgs> Disconnected;
|
public event EventHandler<MqttClientDisconnectedEventArgs> Disconnected;
|
||||||
public event EventHandler<MqttApplicationMessageReceivedEventArgs> ApplicationMessageReceived;
|
public event EventHandler<MqttApplicationMessageReceivedEventArgs> ApplicationMessageReceived;
|
||||||
|
public event EventHandler<ApplicationMessageProcessedEventArgs> ApplicationMessageProcessed;
|
||||||
|
|
||||||
public async Task StartAsync(IManagedMqttClientOptions options)
|
public async Task StartAsync(IManagedMqttClientOptions options)
|
||||||
{
|
{
|
||||||
@@ -245,6 +246,7 @@ namespace MQTTnet.ManagedClient
|
|||||||
|
|
||||||
private async Task TryPublishQueuedMessageAsync(MqttApplicationMessage message)
|
private async Task TryPublishQueuedMessageAsync(MqttApplicationMessage message)
|
||||||
{
|
{
|
||||||
|
Exception transmitException = null;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await _mqttClient.PublishAsync(message).ConfigureAwait(false);
|
await _mqttClient.PublishAsync(message).ConfigureAwait(false);
|
||||||
@@ -256,6 +258,8 @@ namespace MQTTnet.ManagedClient
|
|||||||
}
|
}
|
||||||
catch (MqttCommunicationException exception)
|
catch (MqttCommunicationException exception)
|
||||||
{
|
{
|
||||||
|
transmitException = exception;
|
||||||
|
|
||||||
_logger.Warning<ManagedMqttClient>(exception, "Publishing application message failed.");
|
_logger.Warning<ManagedMqttClient>(exception, "Publishing application message failed.");
|
||||||
|
|
||||||
if (message.QualityOfServiceLevel > MqttQualityOfServiceLevel.AtMostOnce)
|
if (message.QualityOfServiceLevel > MqttQualityOfServiceLevel.AtMostOnce)
|
||||||
@@ -265,8 +269,13 @@ namespace MQTTnet.ManagedClient
|
|||||||
}
|
}
|
||||||
catch (Exception exception)
|
catch (Exception exception)
|
||||||
{
|
{
|
||||||
|
transmitException = exception;
|
||||||
_logger.Error<ManagedMqttClient>(exception, "Unhandled exception while publishing queued application message.");
|
_logger.Error<ManagedMqttClient>(exception, "Unhandled exception while publishing queued application message.");
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
ApplicationMessageProcessed?.Invoke(this, new ApplicationMessageProcessedEventArgs(message, transmitException));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task PushSubscriptionsAsync()
|
private async Task PushSubscriptionsAsync()
|
||||||
|
|||||||
Reference in New Issue
Block a user