Closed BlueGradientHorizon closed 1 year ago
The code is quite large, so I only skimmed through it (it looks like it's totally possible to rewrite the code in a simpler and more concise way to demonstrate the problem), so I'll just go through your questions and try to answer them:
close()
on the client/sink is fine (it will essentially just send and flush the close frame).ConnectionClosed
are generally transparently converted into None
on the receiving stream. You're getting a different error though (IoError
/ ConnectionAborted
), so there is likely something wrong with your application logic.mpsc::unbounded()
. Sending to the channel will succeed as long as the receiver is alive at the moment of sending.If you see that I did something wrong in the code, or not as usual, too complicated, or vice versa, please also let me know.
In terms of complexity, it generally feels like it could be simplified a lot. In terms of "not as usual" - I'd say there are too many mutexes that seem to be redundant (not necessary, could be rewritten in a different way). But this will come with time and practice (and also while getting familiar with Tokio more).
Hope this was helpful!
Hello. I'm trying to write a client-server application, but I'm having some issues/misunderstandings related to handling the WebSocket close event. The source code below includes both the client and the server. I tried to shorten the code as much as I could, but it still turned out to be long, I'm sorry.
Cargo.toml
:main.rs
:To start the server or client, pass the appropriate command line argument:
The point is that when, 7 seconds after the server starts (I did this for a test), it disconnects all connected clients, an error may appear on the client with NOT a 100% chance. You may need to make 5 to 20 attempts to start the server and client to catch this error. Client console output:
I'm on Windows 11. Each time the error code may be slightly different, but the meaning is about the same. Questions:
disconnect()
method ofClient
and line 137 (outgoing.close().await.unwrap();
).while let Some(message) = read.next().await { ... }
)?Message::Close
, should I take some action myself to close the WebSocket connection, or shouldtokio_tungstenite
do it by itself?ConnectionClosed
error from thenext()
method? Because now after receiving the messageMessage::Close
I get the errorIo
.Maybe I didn't see something in the official documentation. I recently started learning the Rust language and the
tokio
andtokio-tungstenite
crates. If you see that I did something wrong in the code, or not as usual, too complicated, or vice versa, please also let me know. Thank you for your time.