reconnect method

Manager reconnect()

Attempt a reconnection.

@api private

Implementation

Manager reconnect() {
  if (reconnecting || skipReconnect) return this;

  if (backoff!.attempts >= _reconnectionAttempts) {
    _logger.fine('reconnect failed');
    backoff!.reset();
    emitAll('reconnect_failed');
    reconnecting = false;
  } else {
    var delay = backoff!.duration;
    _logger.fine('will wait %dms before reconnect attempt', delay);

    reconnecting = true;
    var timer = Timer(Duration(milliseconds: delay.toInt()), () {
      if (skipReconnect) return;

      _logger.fine('attempting reconnect');
      emitAll('reconnect_attempt', backoff!.attempts);
      emitAll('reconnecting', backoff!.attempts);

      // check again for the case socket closed in above events
      if (skipReconnect) return;

      open(callback: ([err]) {
        if (err != null) {
          _logger.fine('reconnect attempt error');
          reconnecting = false;
          reconnect();
          emitAll('reconnect_error', err['data']);
        } else {
          _logger.fine('reconnect success');
          onreconnect();
        }
      });
    });

    subs.add(Destroyable(() => timer.cancel()));
  }
  return this;
}