connect method

Future<StompClient> connect({
  1. required PeerId peerId,
  2. required String hostName,
  3. String? login,
  4. String? passcode,
  5. Duration? timeout,
})

Creates a new STOMP client connection to a peer

Implementation

Future<StompClient> connect({
  required PeerId peerId,
  required String hostName,
  String? login,
  String? passcode,
  Duration? timeout,
}) async {
  if (!_isStarted) {
    throw const StompStateException('Service not started', 'stopped', 'started');
  }

  // Check if we already have a client for this peer
  final existingClient = _clients[peerId];
  if (existingClient != null) {
    if (existingClient.isConnected) {
      return existingClient;
    } else {
      // Remove disconnected client
      _clients.remove(peerId);
    }
  }

  // Create new client
  final client = StompClient(
    host: _host,
    serverPeerId: peerId,
    hostName: hostName,
    login: login,
    passcode: passcode,
    timeout: timeout ?? _options.timeout,
  );

  // Listen for state changes to clean up disconnected clients
  client.onStateChange.listen((state) {
    if (state == StompClientState.disconnected || state == StompClientState.error) {
      _clients.remove(peerId);
    }
  });

  // Connect
  await client.connect();
  _clients[peerId] = client;

  _logger.info('Connected to STOMP server at $peerId');
  return client;
}