connect method

void connect()

Implementation

void connect() {
  if (_closed) return;
  try {
    _channel = WebSocketChannel.connect(Uri.parse(url));

    _channel!.sink.add(jsonEncode(authData));
    _reconnectDelay = 1000;

    _pingTimer?.cancel();
    _pingTimer = Timer.periodic(const Duration(seconds: 30), (timer) {
      if (_channel != null) send({'type': 'ping'});
    });

    _channel!.stream.listen(
      (message) {
        if (!_hasConnectedOnce) {
          _hasConnectedOnce = true;
        }
        _onMessage?.call(MessageEvent(data: message));
      },
      onError: (_) => _handleDisconnect(),
      onDone: () => _handleDisconnect(),
    );
  } catch (_) {
    _handleDisconnect();
  }
}