WebSocketConnection constructor

WebSocketConnection(
  1. WebSocket socket,
  2. ClientLink clientLink, {
  3. Function? onConnect,
  4. bool enableAck = false,
  5. DsCodec? useCodec,
})

clientLink is not needed when websocket works in server link

Implementation

WebSocketConnection(
  this.socket,
  this.clientLink, {
  this.onConnect,
  bool enableAck = false,
  DsCodec? useCodec,
}) {
  if (useCodec != null) {
    codec = useCodec;
  }

  if (!enableAck) {
    nextMsgId = -1;
  }
  socket.binaryType = 'arraybuffer';
  _responderChannel = PassiveChannel(this);
  _requesterChannel = PassiveChannel(this);
  socket.onMessage.listen(_onData, onDone: _onDone);
  socket.onClose.listen(_onDone);
  socket.onOpen.listen(_onOpen);
  // TODO, when it's used in client link, wait for the server to send {allowed} before complete this
  _onRequestReadyCompleter.complete(Future.value(_requesterChannel));

  pingTimer = Timer.periodic(const Duration(seconds: 20), onPingTimer);
}