connect method

  1. @override
Future<void> connect({
  1. String? host,
  2. int? port,
  3. Duration? duration,
  4. Duration? deadline,
})
override

WS connect

Implementation

@override
Future<void> connect(
    {String? host, int? port, Duration? duration, Duration? deadline}) async {
  if (log) {
    loger.log(
      "\u001b[32m${"connecting"}\u001b[0m",
      name: "ws",
    );
  }
  _host = host ?? _host;
  _port = port ?? _port;
  _duration = duration ?? _duration;
  _deadline = deadline ?? _deadline;

  if (status != ConnectStatus.disconnect) {
    return Future.value();
  }
  status = ConnectStatus.connecting;
  try {
    _socket = await getConnectionSocket(duration: duration);
    _remoteInfo = RemoteInfo(
      address: "${credentials.isSecure ? "wss" : "ws"}://$_host:$_port$_path",
      host: _host,
      port: _port,
      family: credentials.isSecure ? "wss" : "ws",
    );
    _localInfo = LocalInfo(
      address: _localInfo.address,
      family: credentials.isSecure ? "wss" : "ws",
      port: _port,
    );
    status = ConnectStatus.connected;
    _onConnect?.call();
  } catch (e) {
    if (log) loger.log("connect error: $e", name: "ws");
    status = ConnectStatus.disconnect;
    _onError?.call(Error.from(e));
    _onClose?.call();
    return Future.value();
  }
  _socket?.pingInterval = deadline;
  _broadcastNotifications();
  return Future.value();
}