Reconnect issue on v3.0.15.0
Hi all, I'm using MQTTNet version 3.0.15.0 with reconnected logic as below. But sometimes inside UseDisconnectedHandler return an exception then seem like UseDisconnectedHandler stop working.
What I expect is even got exception. In next 5s UseDisconnectedHandler is still raised up. I'm I correct, and what is best solution for reconnecting in MQTTNet
My logging
08/09/2021 02:12:55.812: ### DISCONNECTED FROM SERVER ###
08/09/2021 02:13:00.847: Retry to reconnect
08/09/2021 02:13:00.896: ### RECONNECTING FAILED ###
08/09/2021 02:13:00.932: NormalDisconnection
08/09/2021 02:13:00.976: Exception of type 'MQTTnet.Exceptions.MqttCommunicationTimedOutException' was thrown.
My code
public async Task<bool> Connect(string server, int port, string userName, string password, Action onConnected, string clientId = null)
{
_server = server;
_userName = userName;
_password = password;
OnClientConnected = onConnected;
CreateClient();
if (clientId == null)
clientId = $"Locker_Client_{Guid.NewGuid()}";
Log($"Client Id = {clientId}");
var options = new MqttClientOptionsBuilder()
.WithClientId(clientId)
.WithTcpServer(server, port)
.WithCredentials(userName, password)
.WithCleanSession(true)
.Build();
var managedOptions = new ManagedMqttClientOptionsBuilder()
//.WithAutoReconnectDelay(TimeSpan.FromSeconds(5))
.WithClientOptions(options)
.Build();
mqttClient.ConnectedHandler = new MqttClientConnectedHandlerDelegate(OnConnected);
mqttClient.ConnectingFailedHandler = new ConnectingFailedHandlerDelegate(OnConnectingFailed);
mqttClient.UseDisconnectedHandler(async e =>
{
Log("### DISCONNECTED FROM SERVER ###");
await Task.Delay(TimeSpan.FromSeconds(5));
try
{
IsConnected = false;
Log("Retry to reconnect");
await mqttClient.StartAsync(managedOptions);
//await Connect(_server, 1883, _userName, _password, OnClientConnected);
Log("Reconnect done");
}
catch
{
Log("### RECONNECTING FAILED ###");
Log(e.Reason.ToString());
Log(e.Exception.Message);
}
});
mqttClient.UseConnectedHandler(async e =>
{
Log("### CONNECTED WITH SERVER ###");
IsConnected = true;
onConnected?.Invoke();
});
mqttClient.UseApplicationMessageReceivedHandler(e =>
{
var topic = e.ApplicationMessage.Topic;
var payload = Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
Log("### RECEIVED APPLICATION MESSAGE ###");
Log($"+ Topic = {topic}");
Log($"+ Payload = {payload}");
Log($"+ QoS = {e.ApplicationMessage.QualityOfServiceLevel}");
Log($"+ Retain = {e.ApplicationMessage.Retain}");
Log("");
MessageReceivedAction?.Invoke(new MqttMessage { Topic = topic, Payload = payload });
});
await mqttClient.StartAsync(managedOptions);
// var result = await mqttClient.ConnectAsync(options, CancellationToken.None);
//Log("MQTT Connect Result = " + result.ResultCode.ToString());
return true;
}
Hi, just to add on- I'm the poster's colleague We implemented this library in Xamarin Forms for Android-after a period we face disconnection from the server, which is when we apply the UseDisconnectedHandler, but it throws an exception.
We're having a hard time recreating the problem, it happens randomly after a period of time without much activity.
Thanks for any help or insight you can provide!
Please try the same with the regular and not managed client. Then you have to add a loop for reconnect like while(true) > Connect if disconnect -> Delay. Please let me know if this approach has an issue as well.
Closing this due to inactivity.