connect method

Future<void> connect({
  1. Map<String, String>? headers,
})

Connect to the server

Implementation

Future<void> connect({Map<String, String>? headers}) async {
  _logger.info('Client: Starting connection to $_endpoint');
  _logger.info('Client: Current state: $_state');

  if (_state != unconnected) {
    _logger.warning(
        'Client: Cannot connect, not in unconnected state. Current state: $_state');
    return;
  }

  _logger.info('Client: Calling dispatcher.connect()...');

  try {
    await _dispatcher.connect(headers: headers);
    _logger.info('Client: Dispatcher.connect() completed successfully');

    _state = connected;
    _stateController.add(_state);
    _logger.info('Client: State updated to connected and broadcasted');

    _logger.info('Client: Connection completed successfully');
  } catch (e) {
    _logger.severe('Client: Connection failed: $e');
    _state = disconnected;
    _stateController.add(_state);
    _logger.severe('Client: State updated to disconnected due to error');
    rethrow;
  }
}