connect method

dynamic connect()

Establishes a WebSocket connection.

This method establishes a WebSocket connection using a self-signed certificate. It invokes the onOpen callback when the connection is successfully opened. Messages received trigger the onMessage callback, and connection closure invokes onClose.

Implementation

connect() async {
  try {
    _socket = await _connectForSelfSignedCert();
    onOpen?.call();
    _socket?.listen((data) {
      onMessage?.call(data);
    }, onDone: () {
      onClose?.call(_socket?.closeCode, _socket?.closeReason);
    });
  } catch (e) {
    onClose?.call(500, e.toString());
  }
}