Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added a connection timeout callback #523

Open
laterdayi opened this issue Mar 17, 2024 · 8 comments
Open

feat: Added a connection timeout callback #523

laterdayi opened this issue Mar 17, 2024 · 8 comments

Comments

@laterdayi
Copy link

for example

I set the timeout to 10 s, then I will get a callback after 10 s,

mqttClient.timeOut = 10
mqttClient.onConnectTimeOut = (){
  print('Failed to connect successfully after more than 10 seconds')
}

This way I can solve it,If the total connection time exceeds 10S, I can manually disconnect and manually connect it

mqttClient.timeOut = 10
mqttClient.onConnectTimeOut = (){
  mqttClient.disConnect();
  mqttClient.connect()
  print('Failed to connect successfully after more than 10 seconds')
}

@laterdayi laterdayi changed the title Added a connection timeout callback feat: Added a connection timeout callback Mar 17, 2024
@shamblett
Copy link
Owner

From #521 -

OK, so are you asking for a callback to be added in between connection attempts, i.e. if maxConnectionAttempts is 5 then you will trigger the callback on each failed connection attempt. If that's the case raise an issue for this documenting exactly what you want and which use case this will help with and I'll mark it as a feature request.

@laterdayi
Copy link
Author

From #521 -

OK, so are you asking for a callback to be added in between connection attempts, i.e. if maxConnectionAttempts is 5 then you will trigger the callback on each failed connection attempt. If that's the case raise an issue for this documenting exactly what you want and which use case this will help with and I'll mark it as a feature request.

support can set the callback when the connection times out

mqttClient.timeOut = 10
mqttClient.onConnectTimeOut = (){
  print('Failed to connect successfully after more than 10 seconds')
}

@shamblett
Copy link
Owner

There is no field on the client named 'timeOut'. Are you saying that you want a 10 second connection timeout which when expired calls the failed connection callback?

If so you can set maxConnectionAttempts to 1 and _connectTimeoutPeriod to 10000, giving a one shot connection attempt that will last for 10 seconds.

What I'm proposing is that the failed connection callback will be called after every failed attempt, in the case above just once after 10 seconds. If you set maxConnectionAttempts to 3 and _connectTimeoutPeriod to 2000, it will be called 3 times each 2 seconds apart. Note that the attempt number is passed to callback so you can see what attempt failed.

I think I'll disable raising of the NoConnectionException if the user supplies a failed connection callback, clearly in this case they want to handle this eventuality in this way, throwing an exception as well just adds to the noise.

@laterdayi
Copy link
Author

I've set maxConnectionAttempts to 1 and with the current configuration it will always reconnect without any errors, and if the connection is more than 5S, there will be no connection timeouts or connection failures

@laterdayi
Copy link
Author

I set the _connectTimeoutPeriod to 10, but after 10 seconds, I don't get any callbacks for connection failures

@laterdayi
Copy link
Author

I'm now in a scenario where I want to set a timeout of 10s, and if it exceeds 10s, a connection failure callback or a connection timeout callback is triggered

@shamblett
Copy link
Owner

'I set the _connectTimeoutPeriod to 10, but after 10 seconds, I don't get any callbacks for connection failures', no it won't do will it until this change is in the client, this is what I'm doing now.

'I've set maxConnectionAttempts to 1 and with the current configuration it will always reconnect without any errors,' this update is meant for when there is no connection, not when there is, the behaviour of the client is different in these cases.

'I'm now in a scenario where I want to set a timeout of 10s, and if it exceeds 10s, a connection failure callback or a connection timeout callback is triggered', as I've said above when this change is in you will be able to set maxConnectionAttempts to 1 and _connectTimeoutPeriod to 10000 and if you supply a failed connection attempt callback you will get the callback triggered after 10 seconds.

@shamblett
Copy link
Owner

Package updated in line with the above commentary and re released at version 10.3.0.

Please see the onFailedConnectionAttempt callback API in the client, there is also an example of usage mqtt_server_client_failed_connection.dart in the examples directory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants