Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how does WebSocketChannel warns that it disconnected?

Tags:

dart

dart-2

I am using WebSocketChannel as a socket server:

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

How can I know when the webSocket above gets disconnected?

like image 631
Jonathan Avatar asked Oct 25 '25 04:10

Jonathan


1 Answers

You have to listen on the channel stream and intercept the close event with the onDone callback.

closeCode and closeReason properties give you details about the close.

webSocketHandler((channel) {
  channel.stream.listen((data) {
    channel.sink.add('Echo: $data');
  },
  onDone: () {
    print('socket closed: reason=[${channel.closeReason}], code:[${channel.closeCode}]');
  });
});
like image 52
attdona Avatar answered Oct 26 '25 19:10

attdona