connect method

Future<void> connect()

Connects to the WebSocket server

Implementation

Future<void> connect() async {
  if (_isConnected) return;

  try {
    _channel = WebSocketChannel.connect(Uri.parse(wsUrl));

    _subscription = _channel!.stream.listen(
      _handleMessage,
      onError: _handleError,
      onDone: _handleDone,
    );

    _isConnected = true;
    _reconnectAttempts = 0;
    _connectionController.add(true);

    // Authenticate if we have a token
    if (tokenStorage.accessToken != null) {
      await authenticate(tokenStorage.accessToken!);
    }
  } catch (e) {
    _isConnected = false;
    _connectionController.add(false);
    _scheduleReconnect();
    rethrow;
  }
}