Skip to content

Commit

Permalink
Improve disconnection and reconnection (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ellerbach authored Oct 27, 2022
1 parent 295d334 commit f589624
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions nanoFramework.Azure.Devices.Client/DeviceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class DeviceClient : IDisposable
private readonly string _deviceMessageTopic;
private Twin _twin;
private bool _twinReceived;
private MqttClient _mqttc;
private MqttClient _mqttc = null;
private readonly IoTHubStatus _ioTHubStatus = new IoTHubStatus();
private readonly ArrayList _methodCallback = new ArrayList();
private readonly ArrayList _waitForConfirmation = new ArrayList();
Expand Down Expand Up @@ -279,19 +279,24 @@ private void TimerCallbackReconnect(object state)
/// </summary>
public void Close()
{
if (_mqttc.IsConnected)
if (_mqttc != null)
{
_mqttc.Unsubscribe(new[] {
if (_mqttc.IsConnected)
{
_mqttc.Unsubscribe(new[] {
$"devices/{_deviceId}/messages/devicebound/#",
"$iothub/twin/#",
"$iothub/methods/POST/#"
});
}

_mqttc.Disconnect();
_mqttc.Close();
// Make sure all get disconnected, cleared
Thread.Sleep(1000);
}

_timerTokenRenew.Dispose();
_timerTokenRenew?.Dispose();
}

/// <summary>
Expand Down

0 comments on commit f589624

Please sign in to comment.