open method

  1. @override
Future<void> open()
override

Opens the underlying connection. Idempotent.

Implementation

@override
Future<void> open() async {
  if (_closed) {
    throw StateError('WebSocketOpcUaByteTransport already closed');
  }
  if (_opened) return;
  final wsUri = _toWsUri(endpoint);
  final fut =
      WebSocket.connect(wsUri.toString(), protocols: subprotocols);
  final ws = await (connectTimeout == null
      ? fut
      : fut.timeout(connectTimeout!));
  _ws = ws;
  _rxSub = ws.listen(
    (event) {
      if (_rxCtrl.isClosed) return;
      if (event is List<int>) {
        _rxCtrl.add(
          event is Uint8List ? event : Uint8List.fromList(event),
        );
      }
      // Text frames are not part of the OPC UA Conversation
      // Protocol; ignore.
    },
    onError: (Object e, StackTrace st) {
      if (!_rxCtrl.isClosed) _rxCtrl.addError(e, st);
    },
    onDone: () {
      if (!_closed) {
        _closed = true;
        if (!_rxCtrl.isClosed) _rxCtrl.close();
      }
    },
    cancelOnError: false,
  );
  _opened = true;
}