connect method

void connect({
  1. Iterable<String>? protocols,
  2. required WebSocketSettings webSocketSettings,
})

Implementation

void connect(
    {Iterable<String>? protocols,
    required WebSocketSettings webSocketSettings}) async {
  logger.info('connect $_url, ${webSocketSettings.extraHeaders}, $protocols');
  try {
    if (webSocketSettings.allowBadCertificate) {
      /// Allow self-signed certificate, for test only.
      _socket = await _connectForBadCertificate(_url, webSocketSettings);
    } else {
      _socket = await WebSocket.connect(_url,
          protocols: protocols, headers: webSocketSettings.extraHeaders);
    }

    onOpen?.call();
    _socket!.listen((dynamic data) {
      onMessage?.call(data);
    }, onDone: () {
      onClose?.call(_socket!.closeCode, _socket!.closeReason);
    });
  } catch (e) {
    onClose?.call(500, e.toString());
  }
}