onConnectionLost method

void onConnectionLost([
  1. Object? e
])

Implementation

void onConnectionLost([Object? e]) async {
  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();

  if (_connectionStateController.isClosed || _wasDisposed) {
    return;
  }

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

  if (config.autoReconnect &&
      !_connectionStateController.isClosed &&
      !_wasDisposed) {
    if (config.delayBetweenReconnectionAttempts != null) {
      _reconnectTimer = Timer(
        config.delayBetweenReconnectionAttempts!,
        () {
          _connect();
        },
      );
    } else {
      Timer.run(() => _connect());
    }
  }
}