Skip to content

Commit fa506c3

Browse files
committed
Improve disconnection and reconnection
1 parent 786839a commit fa506c3

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

nanoFramework.Aws.IoTCore.Devices/MqttConnectionClient.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public enum QoSLevel
5151
private readonly X509Certificate2 _clientCert; //Combined ClientRsaSha256Crt and ClientRsaKey
5252
private readonly X509Certificate _awsRootCACert;
5353

54-
private M2Mqtt.MqttClient _mqttc;
54+
private M2Mqtt.MqttClient _mqttc = null;
5555
private readonly ConnectorState _mqttBrokerStatus = new ConnectorState();
5656
private readonly ArrayList _waitForConfirmation = new ArrayList();
5757
private readonly object _lock = new object();
@@ -243,18 +243,22 @@ private void TimerCallbackReconnect(object state)
243243
/// </summary>
244244
public void Close()
245245
{
246-
if (_mqttc.IsConnected)
246+
if (_mqttc != null)
247247
{
248-
_mqttc.Unsubscribe(new[] {
248+
if (_mqttc.IsConnected)
249+
{
250+
_mqttc.Unsubscribe(new[] {
249251
$"{_deviceMessageTopic}/#",
250252
$"{ _shadowTopic }/#",
251253
});
254+
}
252255
_mqttc.Disconnect();
256+
_mqttc.Close();
253257
// Make sure all get disconnected, cleared (TODO: 1 second arbitrary value specified)
254258
Thread.Sleep(1000);
255259
}
256260

257-
_timerTokenRenew.Dispose();
261+
_timerTokenRenew?.Dispose();
258262
}
259263

260264
/// <summary>

0 commit comments

Comments
 (0)