connect method

void connect()

Open the connection (idempotent). Rooms are re-joined after a reconnect.

Implementation

void connect() {
  if (_socket != null) return;
  NexusLog.debug('realtime connecting → ${_cfg.socketBase}');
  final socket = io.io(
    _cfg.socketBase,
    io.OptionBuilder()
        .setTransports(['websocket', 'polling'])
        .disableAutoConnect()
        .setAuth({
          'key': _cfg.apiKey,
          if (_id.distinctId != null) 'distinctId': _id.distinctId,
          'sessionKey': _id.sessionKey,
          'deviceKey': _id.deviceKey,
          'os': _id.deviceContext['osType'],
          'osVersion': _id.deviceContext['osVersion'],
        })
        .setExtraHeaders({'x-api-key': _cfg.apiKey})
        .build(),
  );
  // Register any handlers added before the socket existed; they persist across
  // this socket's reconnects.
  _handlers.forEach((event, handler) => socket.on(event, handler));
  socket.onConnect((_) {
    NexusLog.info('realtime connected (${socket.id})');
    for (final r in _rooms) {
      NexusLog.debug('realtime re-joining "$r" after (re)connect');
      socket.emitWithAck('room.join', {
        'name': r,
      }, ack: (dynamic res) => NexusLog.debug('realtime join "$r" ← $res'));
    }
  });
  socket.onDisconnect((_) => NexusLog.info('realtime disconnected'));
  socket.onConnectError((e) => NexusLog.warn('realtime connect_error: $e'));
  _socket = socket;
  socket.connect();
}