connect method

Future<void> connect()

Connects to the WebSocket server.

Implementation

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

  _eventController.add(WebSocketManagerEvent.connecting());

  try {
    _connection = WebSocketConnection(config);

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

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

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

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

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