sdroege / async-tungstenite

Async binding for Tungstenite, the Lightweight stream-based WebSocket implementation
MIT License
396 stars 61 forks source link

How to close connection gracefully after WebSocketStream.split()? #109

Closed PieceOfGood closed 2 years ago

PieceOfGood commented 2 years ago

For a simple situation, the close() method needs to pass CloseFrame:

let close_frame = CloseFrame {code: CloseCode::Normal, reason: Cow::from("")};
ws_stream.close(close_frame).await;

But if I call split() for ws_stream to create tasks with their participation, method close() stops taking arguments, what throws an exception on the echo server side, like this: websockets.exceptions.ConnectionClosedError: received 1005 (no status code [internal]); then sent 1005 (no status code [internal]) The server continues to run, but in its console it is always an exception when the connection is terminated in the last way.

sdroege commented 2 years ago

You can pass a Message::Close through the Sink part. If you look at the code of WebSocketStream::close, that's literally what it does :)

Sink::close comes from SinkExt and just closes the connection directly.

Does that help?

PieceOfGood commented 2 years ago

Yes, that's what I need. Thanks a lot!