reconnect method

void reconnect({
  1. bool reset = false,
})

Reconnects websocket

Implementation

void reconnect({bool reset = false}) {
  if (reset) {
    _reconnectTimer?.cancel();
    _reconnectTimer = null;
    _state.resetReconnectTask();
  }

  if (_reconnectTimer != null) return;

  final task = _state.reconnectTask;
  if (task == null) throw MisConfigurationError();
  if (task.exceedRetryCount && task.hasRetriedLastChance) {
    task.increaseRetryCount();
    _state.reconnecting = false;
    _eventManager.notifyReconnectionFailed();
    return;
  }

  final backoffPeriod = task.backOffPeriod;
  task.increaseRetryCount();
  _state.reconnecting = true;

  _reconnectTimer = Timer(Duration(seconds: backoffPeriod), () async {
    logger.i('try to reconnect');
    try {
      await connect(
        reconnect: true,
        userId: _state.userId ?? '',
        accessToken: _sessionManager.accessToken,
      );
    } catch (e) {
      _eventManager.notifyReconnectionCanceled();
      _state.reconnecting = false;
      logger.e('reconnecting error: $e');
    }
    logger.i('reconnect succeeded');
  });
}