connect method

Future<void> connect({
  1. Duration? timeout,
})

Attempt to make a connection with the remote server.

A maximum timeout can be optionally specified. Throws a StateError if the client is already connected.

Implementation

Future<void> connect({Duration? timeout}) async {
  if (_hasConnected) {
    throw StateError('Client is already connected');
  }

  _socket = await Socket.connect(
    host,
    port,
    timeout: timeout,
  );

  _hasConnected = true;

  onConnect?.call(_socket);

  _socket.listen(
    (data) => onReceive == null ? null : onReceive!(_socket, data),
    onDone: onDisconnect == null ? null : () => onDisconnect!(_socket),
  );
}