shamblett / mqtt_client

A server and browser based MQTT client for dart
Other
548 stars 176 forks source link

Unable to catch exception + disable all auto-rec* #218

Closed 1N50MN14 closed 4 years ago

1N50MN14 commented 4 years ago

Hey @shamblett,

Thank you for the incredible work on the library!

I'm facing couple of issues:

  1. The following code breaks (debug mode) as in I'm unable to catch the exception despite the catch clause. Makes debugging slightly harder.
    try { await client.connect();  } on Exception catch (e) { debugPrint('Client exception - $e');  }
  2. Upon client.disconnect() or MQTT server outright rejecting the connection (say unauthorized error), mqtt_client keeps trying to re-establish the connection even though I haven't enabled auto reconnect , until the maximum number of tries have been reached before it finally throws an exception, for example:

The maximum allowed connection attempts ({3}) were exceeded. The broker is not responding to the connection request message correctlyThe return code is MqttConnectReturnCode.badUsernameOrPassword.

Apologies forehand if I'm missing on something here. Is this an intended behavior? I haven't enabled auto-reconnect.

Is there any way to disable all auto re-connect, re-subscribe, re-****, max connection attempts and so on and leave it to userland? I've seen the other issues, their need is absolutely understood and appreciated, but they should be opt-in imho.

Many thanks

shamblett commented 4 years ago

You can set maxConnectionAttempts to any value you wish when you create the server or browser client, see the API docs, in your case set it to 1. This is not auto reconnect, it tries maxConnectionAttempts on initial connect, auto reconnect(if enabled) can only be invoked after an initial successful connection,, also the auto resubscribe on auto reconnect can be disabled, again see the API docs.

I understand your comment about opt-in, as many users a s there are like you who want fine grained control there are an equal number who just want things to happen automatically. I have to tread a thin line between the two. In practice, over time, this just adds a plethora of options the user has to set.

Not sure why you can't catch the exception, I'll try this myself and report back.

1N50MN14 commented 4 years ago

I totally missed maxConnectionAttempts it was right in front of my eyes apologies about that.

client.resubscribeOnAutoReconnect = false doesn't seem to be available in the version published on pub.dev could explain why it didn't work for me. Either ways, your feedback totally clarifies things for me, thank you.

Yes the exception is a bit strange, this is the full error message (I'm intentionally shutting down the server): Exception has occurred. SocketException (SocketException: OS Error: Connection refused, errno = 111, address = 10.0.2.2, port = 53212)

It could be Flutter-specific, I'm not sure, I'm relatively new to Flutter/Dart land.

I'm using mqtt_client within a mobile environment. Node-based MQTT server/custom auth logic. Mqtt_client worked like charm with that right of the bat! Absolutely no issues.

Thanks again.

shamblett commented 4 years ago

Client re-published at 8.0.0, please use this version for any further testing. If you have any further issues please report them seperately.

1N50MN14 commented 4 years ago

@shamblett Thank you very much for the fixes!