autoReconnect method Null safety

  1. @protected
void autoReconnect(
  1. AutoReconnect reconnectEvent
)

Auto reconnect

Implementation

@protected
void autoReconnect(AutoReconnect reconnectEvent) async {
  MqttLogger.log('MqttConnectionHandlerBase::autoReconnect entered');
  // If already in progress exit and we were not connected return
  if (autoReconnectInProgress! && !reconnectEvent.wasConnected) {
    return;
  }
  autoReconnectInProgress = true;
  // If the auto reconnect callback is set call it
  if (onAutoReconnect != null) {
    onAutoReconnect!();
  }

  // If we are connected disconnect from the broker.
  if (reconnectEvent.wasConnected) {
    MqttLogger.log(
        'MqttConnectionHandlerBase::autoReconnect - was connected, sending disconnect');
    sendMessage(MqttDisconnectMessage());
    connectionStatus.state = MqttConnectionState.disconnecting;
  }
  connection.disconnect(auto: true);
  connection.onDisconnected = null;
  MqttLogger.log(
      'MqttConnectionHandlerBase::autoReconnect - attempting reconnection');
  connectionStatus = await connect(server, port, connectionMessage);
  autoReconnectInProgress = false;
  if (connectionStatus.state == MqttConnectionState.connected) {
    connection.onDisconnected = onDisconnected;
    // Fire the re subscribe event.
    clientEventBus!.fire(Resubscribe(fromAutoReconnect: true));
    MqttLogger.log(
        'MqttConnectionHandlerBase::autoReconnect - auto reconnect complete');
    // If the auto reconnect callback is set call it
    if (onAutoReconnected != null) {
      onAutoReconnected!();
    }
  } else {
    MqttLogger.log(
        'MqttConnectionHandlerBase::autoReconnect - auto reconnect failed - re trying');
    clientEventBus!.fire(AutoReconnect());
  }
}