WebSocketChannel 如何警告它已断开连接?

how does WebSocketChannel warns that it disconnected?

我正在使用 WebSocketChannel 作为套接字服务器:

var handler = webSocketHandler((WebSocketChannel webSocket) async {
}

我怎么知道上面的 webSocket 何时断开连接?

即使认为这个线程有正确的答案,我最终还是使用了另一个包来处理套接字连接: https://pub.dartlang.org/packages/socket_io

您必须监听频道流并使用 onDone 回调拦截关闭事件。

closeCodecloseReason 属性为您提供有关收盘价的详细信息。

webSocketHandler((channel) {
  channel.stream.listen((data) {
    channel.sink.add('Echo: $data');
  },
  onDone: () {
    print('socket closed: reason=[${channel.closeReason}], code:[${channel.closeCode}]');
  });
});