From 069c19aab94a82cc6a844805622085465b741ed8 Mon Sep 17 00:00:00 2001 From: PMExtra Date: Mon, 23 Dec 2019 20:38:29 +0800 Subject: [PATCH] Support existing session with ManagedMqttClient. --- .../ManagedMqttClient.cs | 14 ++++++++------ .../ReconnectionResult.cs | 1 + 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/Source/MQTTnet.Extensions.ManagedClient/ManagedMqttClient.cs b/Source/MQTTnet.Extensions.ManagedClient/ManagedMqttClient.cs index 89efe6b..4767f4a 100644 --- a/Source/MQTTnet.Extensions.ManagedClient/ManagedMqttClient.cs +++ b/Source/MQTTnet.Extensions.ManagedClient/ManagedMqttClient.cs @@ -91,10 +91,6 @@ namespace MQTTnet.Extensions.ManagedClient if (options == null) throw new ArgumentNullException(nameof(options)); if (options.ClientOptions == null) throw new ArgumentException("The client options are not set.", nameof(options)); - if (!options.ClientOptions.CleanSession) - { - throw new NotSupportedException("The managed client does not support existing sessions."); - } if (!_maintainConnectionTask?.IsCompleted ?? false) throw new InvalidOperationException("The managed client is already started."); @@ -333,6 +329,12 @@ namespace MQTTnet.Extensions.ManagedClient return; } + if (connectionState == ReconnectionResult.Recovered) + { + StartPublishing(); + return; + } + if (connectionState == ReconnectionResult.StillConnected) { await PublishSubscriptionsAsync(Options.ConnectionCheckInterval, cancellationToken).ConfigureAwait(false); @@ -544,8 +546,8 @@ namespace MQTTnet.Extensions.ManagedClient try { - await _mqttClient.ConnectAsync(Options.ClientOptions).ConfigureAwait(false); - return ReconnectionResult.Reconnected; + var result = await _mqttClient.ConnectAsync(Options.ClientOptions).ConfigureAwait(false); + return result.IsSessionPresent ? ReconnectionResult.Recovered : ReconnectionResult.Reconnected; } catch (Exception exception) { diff --git a/Source/MQTTnet.Extensions.ManagedClient/ReconnectionResult.cs b/Source/MQTTnet.Extensions.ManagedClient/ReconnectionResult.cs index fa876c3..092662f 100644 --- a/Source/MQTTnet.Extensions.ManagedClient/ReconnectionResult.cs +++ b/Source/MQTTnet.Extensions.ManagedClient/ReconnectionResult.cs @@ -4,6 +4,7 @@ { StillConnected, Reconnected, + Recovered, NotConnected } }