dart-lang / web_socket_channel

StreamChannel wrappers for WebSockets.
https://pub.dev/packages/web_socket_channel
BSD 3-Clause "New" or "Revised" License
412 stars 107 forks source link

WebSocketChannel has unexpected close code #369

Open dan461 opened 1 month ago

dan461 commented 1 month ago

I'm attempting to use the onDone() callback to detect an unexpected web socket connection loss, and I'm not seeing a consistent close code. I don't know if this is a bug with the plugin, or if I'm doing something wrong. When intentionally closing the connection at the end of a session I call myChannel.sink.close(1000). When I check the channel's close code in my onDone callback I get either 1000 or 1005. I'm expecting 1000 and not 1005 and don't understand why why I would be getting that. I'm not sure if this is unexpected plugin behavior or if it's the peer on the other end not returning a consistent code. Does the channel's close code get set to the code returned by the server, or should it match what I used when closing the sink? Is there a better way to detect a dropped connection?

Here's an example of the callback ` void _handleOnDone() {

if(_channel.closeCode != 1000){
  _onDoneCallback?.call();
}

} `