onConnectionLost method

void onConnectionLost([
  1. Object? e
])

Implementation

void onConnectionLost([Object? e]) async {
  var code = socketChannel?.closeCode;
  var reason = socketChannel?.closeReason;

  await _closeSocketChannel();
  if (e != null) {
    print('There was an error causing connection lost: $e');
  }
  print('Disconnected from websocket.');
  _reconnectTimer?.cancel();
  _pingTimer?.cancel();
  _keepAliveSubscription?.cancel();
  _messageSubscription?.cancel();

  //TODO: do we really need this check here because few lines bellow there is another check
  if (_connectionStateController.isClosed || _wasDisposed) {
    return;
  }

  _connectionWasLost = true;
  _subscriptionInitializers.values.forEach((s) => s.hasBeenTriggered = false);

  if (_isReconnectionPaused ||
      !config.autoReconnect ||
      _connectionStateController.isClosed ||
      _wasDisposed) {
    return;
  }

  var duration = config.delayBetweenReconnectionAttempts ?? Duration.zero;
  if (config.onConnectionLost != null) {
    duration = (await config.onConnectionLost!(code, reason)) ?? duration;
  }

  _reconnectTimer = Timer(
    duration,
    () async {
      _connect();
    },
  );
}