connect method

Future<void> connect()

Connects to the WebSocket server.

Implementation

Future<void> connect() async {
  if (_connection != null && _connection!.isActive) {
    return;
  }

  _addEvent(WebSocketManagerEvent.connecting());

  try {
    _connection = WebSocketConnection(config);

    // Listen to connection state changes
    _stateSubscription = _connection!.stateStream.listen(
      _handleConnectionStateChange,
    );

    // Listen to connection events
    _eventSubscription = _connection!.eventStream.listen(
      _handleConnectionEvent,
    );

    // Connect to the server
    await _connection!.connect();

    // Process queued messages
    _processQueuedMessages();
  } catch (e) {
    _addEvent(WebSocketManagerEvent.connectionFailed(e.toString()));

    if (config.enableReconnection) {
      _scheduleReconnection();
    }
  }
}