From 7de1634a369729a97005314dc43b3fffc9bced97 Mon Sep 17 00:00:00 2001 From: Christian Kratky Date: Wed, 20 Sep 2017 21:05:13 +0200 Subject: [PATCH] Fix issues with disconnected event --- MQTTnet.Core/Client/MqttClient.cs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/MQTTnet.Core/Client/MqttClient.cs b/MQTTnet.Core/Client/MqttClient.cs index f07a85d..799da52 100644 --- a/MQTTnet.Core/Client/MqttClient.cs +++ b/MQTTnet.Core/Client/MqttClient.cs @@ -42,14 +42,14 @@ namespace MQTTnet.Core.Client try { - MqttTrace.Verbose(nameof(MqttClient), "Trying to connect with server."); - await _adapter.ConnectAsync(_options.DefaultCommunicationTimeout, _options).ConfigureAwait(false); - MqttTrace.Verbose(nameof(MqttClient), "Connection with server established."); - _cancellationTokenSource = new CancellationTokenSource(); _latestPacketIdentifier = 0; _packetDispatcher.Reset(); + MqttTrace.Verbose(nameof(MqttClient), "Trying to connect with server."); + await _adapter.ConnectAsync(_options.DefaultCommunicationTimeout, _options).ConfigureAwait(false); + MqttTrace.Verbose(nameof(MqttClient), "Connection with server established."); + StartReceivePackets(_cancellationTokenSource.Token); var connectPacket = new MqttConnectPacket @@ -69,12 +69,13 @@ namespace MQTTnet.Core.Client } MqttTrace.Verbose(nameof(MqttClient), "MQTT connection with server established."); - Connected?.Invoke(this, EventArgs.Empty); if (_options.KeepAlivePeriod != TimeSpan.Zero) { StartSendKeepAliveMessages(_cancellationTokenSource.Token); } + + Connected?.Invoke(this, EventArgs.Empty); } catch (Exception) {