shamblett / mqtt_client

A server and browser based MQTT client for dart
Other
552 stars 179 forks source link

How to determine the cause of the disconnect? #534

Closed BeesUser closed 4 months ago

BeesUser commented 6 months ago

How to determine the cause of the disconnect? I need to understand that my session has been expired and I need to update my login and password client.onDisconnected = () { debugPrint('MQTT_LOGS::Mosquitto onDisconnected....'); }; in callback i dont have any reason

shamblett commented 6 months ago

The client can only give you two reasons for disconnection, solicited or unsolicited.

A solicited disconnect is one where you have called disconnect() on the client an unsolicited disconnect is one that has not been requested e.g. network fail, broker disconnect etc. In the case of an unsolicited disconnect initiated by the broker the client is not told why this has occurred, the reason for the disconnect should be in your broker logs.

You can see how to check for this in the examples, see mqtt_server_client.dart -

void onDisconnected() {
  print('EXAMPLE::OnDisconnected client callback - Client disconnection');
  if (client.connectionStatus!.disconnectionOrigin ==
      MqttDisconnectionOrigin.solicited) {
    print('EXAMPLE::OnDisconnected callback is solicited, this is correct');
  } else {
    print(
        'EXAMPLE::OnDisconnected callback is unsolicited or none, this is incorrect - exiting');
    exit(-1);
  } .......