mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-29 09:52:55 +08:00
Add new server option to disable/enable persistent sessions.
This commit is contained in:
@@ -5,6 +5,9 @@ namespace MQTTnet.Server
|
|||||||
public interface IMqttServerOptions
|
public interface IMqttServerOptions
|
||||||
{
|
{
|
||||||
int ConnectionBacklog { get; }
|
int ConnectionBacklog { get; }
|
||||||
|
|
||||||
|
bool EnablePersistentSessions { get; }
|
||||||
|
|
||||||
int MaxPendingMessagesPerClient { get; }
|
int MaxPendingMessagesPerClient { get; }
|
||||||
MqttPendingMessagesOverflowStrategy PendingMessagesOverflowStrategy { get; }
|
MqttPendingMessagesOverflowStrategy PendingMessagesOverflowStrategy { get; }
|
||||||
|
|
||||||
|
|||||||
@@ -107,6 +107,11 @@ namespace MQTTnet.Server
|
|||||||
_logger.Error(exception, exception.Message);
|
_logger.Error(exception, exception.Message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!_options.EnablePersistentSessions)
|
||||||
|
{
|
||||||
|
await DeleteSessionAsync(clientId).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
|
||||||
Server.OnClientDisconnected(new ConnectedMqttClient
|
Server.OnClientDisconnected(new ConnectedMqttClient
|
||||||
{
|
{
|
||||||
ClientId = clientId,
|
ClientId = clientId,
|
||||||
@@ -224,6 +229,20 @@ namespace MQTTnet.Server
|
|||||||
return context.ReturnCode;
|
return context.ReturnCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async Task DeleteSessionAsync(string clientId)
|
||||||
|
{
|
||||||
|
await _sessionsLock.EnterAsync(CancellationToken.None);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_sessions.Remove(clientId);
|
||||||
|
_logger.Verbose("Session for client '{0}' deleted.", clientId);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_sessionsLock.Exit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private async Task<GetOrCreateClientSessionResult> PrepareClientSessionAsync(MqttConnectPacket connectPacket)
|
private async Task<GetOrCreateClientSessionResult> PrepareClientSessionAsync(MqttConnectPacket connectPacket)
|
||||||
{
|
{
|
||||||
await _sessionsLock.EnterAsync(CancellationToken.None).ConfigureAwait(false);
|
await _sessionsLock.EnterAsync(CancellationToken.None).ConfigureAwait(false);
|
||||||
|
|||||||
@@ -8,10 +8,11 @@ namespace MQTTnet.Server
|
|||||||
|
|
||||||
public MqttServerTlsEndpointOptions TlsEndpointOptions { get; } = new MqttServerTlsEndpointOptions();
|
public MqttServerTlsEndpointOptions TlsEndpointOptions { get; } = new MqttServerTlsEndpointOptions();
|
||||||
|
|
||||||
|
public bool EnablePersistentSessions { get; set; }
|
||||||
|
|
||||||
public int ConnectionBacklog { get; set; } = 10;
|
public int ConnectionBacklog { get; set; } = 10;
|
||||||
|
|
||||||
public int MaxPendingMessagesPerClient { get; set; } = 250;
|
public int MaxPendingMessagesPerClient { get; set; } = 250;
|
||||||
|
|
||||||
public MqttPendingMessagesOverflowStrategy PendingMessagesOverflowStrategy { get; set; } = MqttPendingMessagesOverflowStrategy.DropOldestQueuedMessage;
|
public MqttPendingMessagesOverflowStrategy PendingMessagesOverflowStrategy { get; set; } = MqttPendingMessagesOverflowStrategy.DropOldestQueuedMessage;
|
||||||
|
|
||||||
public TimeSpan DefaultCommunicationTimeout { get; set; } = TimeSpan.FromSeconds(15);
|
public TimeSpan DefaultCommunicationTimeout { get; set; } = TimeSpan.FromSeconds(15);
|
||||||
|
|||||||
@@ -103,6 +103,12 @@ namespace MQTTnet.Server
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public MqttServerOptionsBuilder WithPersistentSessions()
|
||||||
|
{
|
||||||
|
_options.EnablePersistentSessions = true;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public IMqttServerOptions Build()
|
public IMqttServerOptions Build()
|
||||||
{
|
{
|
||||||
return _options;
|
return _options;
|
||||||
|
|||||||
Reference in New Issue
Block a user