connect method

Future connect()

Implementation

Future connect() async {
  if (isDisposed) {
    socketNotifier = SocketNotifier();
  }
  try {
    connectionStatus = ConnectionStatus.connecting;
    socket = allowSelfSigned
        ? await _connectForSelfSignedCert(url)
        : await WebSocket.connect(url);

    socket!.pingInterval = ping;
    socketNotifier?.open?.call();
    connectionStatus = ConnectionStatus.connected;

    socket!.listen(
      (data) {
        socketNotifier!.notifyData(data);
      },
      onError: (err) {
        socketNotifier!.notifyError(Close(err.toString(), 1005));
      },
      onDone: () {
        connectionStatus = ConnectionStatus.closed;
        socketNotifier!.notifyClose(
          Close('Connection Closed', socket!.closeCode),
        );
      },
      cancelOnError: true,
    );
    return;
  } on SocketException catch (e) {
    connectionStatus = ConnectionStatus.closed;
    socketNotifier!.notifyError(
      Close(e.osError!.message, e.osError!.errorCode),
    );
    return;
  }
}