mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-18 10:26:11 +08:00
Added interceptor for undelivered messages.
This commit is contained in:
@@ -29,5 +29,7 @@ namespace MQTTnet
|
||||
public byte[] CorrelationData { get; set; }
|
||||
|
||||
public List<uint> SubscriptionIdentifiers { get; set; }
|
||||
|
||||
public uint DeliveryCount { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,5 +25,7 @@ namespace MQTTnet.Server
|
||||
IMqttServerStorage Storage { get; }
|
||||
|
||||
IMqttRetainedMessagesManager RetainedMessagesManager { get; }
|
||||
|
||||
IMqttServerApplicationMessageInterceptor UndeliveredMessageInterceptor { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -56,6 +56,8 @@ namespace MQTTnet.Server
|
||||
|
||||
_logger.Verbose("Queued application message with topic '{0}' (ClientId: {1}).", applicationMessage.Topic, ClientId);
|
||||
|
||||
applicationMessage.DeliveryCount++;
|
||||
|
||||
ApplicationMessagesQueue.Enqueue(applicationMessage, senderClientId, checkSubscriptionsResult.QualityOfServiceLevel, isRetainedApplicationMessage);
|
||||
}
|
||||
|
||||
|
||||
@@ -224,6 +224,8 @@ namespace MQTTnet.Server
|
||||
await _retainedMessagesManager.HandleMessageAsync(sender?.ClientId, applicationMessage).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
applicationMessage.DeliveryCount = 0;
|
||||
|
||||
foreach (var clientSession in _sessions.Values)
|
||||
{
|
||||
clientSession.EnqueueApplicationMessage(
|
||||
@@ -231,6 +233,16 @@ namespace MQTTnet.Server
|
||||
sender?.ClientId,
|
||||
false);
|
||||
}
|
||||
|
||||
if (applicationMessage.DeliveryCount == 0)
|
||||
{
|
||||
if (_options.UndeliveredMessageInterceptor == null)
|
||||
{
|
||||
throw new OperationCanceledException(nameof(_options.UndeliveredMessageInterceptor));
|
||||
}
|
||||
|
||||
await _options.UndeliveredMessageInterceptor.InterceptApplicationMessagePublishAsync(new MqttApplicationMessageInterceptorContext(sender?.ClientId, sender?.Session?.Items, applicationMessage));
|
||||
}
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
|
||||
@@ -31,5 +31,7 @@ namespace MQTTnet.Server
|
||||
public IMqttServerStorage Storage { get; set; }
|
||||
|
||||
public IMqttRetainedMessagesManager RetainedMessagesManager { get; set; } = new MqttRetainedMessagesManager();
|
||||
|
||||
public IMqttServerApplicationMessageInterceptor UndeliveredMessageInterceptor { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -212,5 +212,11 @@ namespace MQTTnet.Server
|
||||
{
|
||||
return _options;
|
||||
}
|
||||
|
||||
public MqttServerOptionsBuilder WithUndeliveredMessageInterceptor(Action<MqttApplicationMessageInterceptorContext> value)
|
||||
{
|
||||
_options.UndeliveredMessageInterceptor = new MqttServerApplicationMessageInterceptorDelegate(value);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user