nimiq / core-rs

Official Rust implementation of the Nimiq protocol
https://nimiq.com
Other
74 stars 10 forks source link

Closing WebSocket connections #10

Closed paberr closed 5 years ago

paberr commented 5 years ago

Currently, we close our WebSocket connections by:

  1. Having a select between a one-shot channel and the connection processing future.
  2. Sending a close event via the one-shot channel and thus terminating the connection processing future.
  3. Sending the WebSocket close frame after having terminated the connection processing future.

https://github.com/nimiq/core-rs/blob/52906bda31a837966964d416b8c9c0436b874248/network/src/connection/network_connection.rs#L131-L159

With the most recent version of tungstenite and tokio-tungstenite there might be a nicer way to do it:

I only skimmed over the two pull requests, but it seems that it is now possible to send the close frame via the Sink and shut down the Stream part when receiving the other party's close frame.

daniel-abramov commented 5 years ago

Hi,

just noticed this issue by accident. I would like to just inform you that we've recently updated both tungstenite-rs and tokio-tungstenite on crates.io, so if you want, you can use the latest version from crates.io which now have the latest changes and fixes and also the semantics which does seem to suite your needs (i.e. you can receive the Message::Close and also push it to the sink if needed). Also the Async::Ready(None) is returned by tokio-tungstenite's Stream when the connection is meant to be closed (i.e. when the object can safely be dropped).