-
I'm trying to figure out how to recover from disconnections from a websocket by the client. I have browser client that listens to incoming messages and can send them. I want to be able to close that client or reload it and then connect to the websocket again afterward and start receiving messages again. But right now in my websocket handler I just bail out of the loop: loop {
if let Some(value) = world_info_rx.lock().await.recv().await {
let json = serde_json::to_string(&value).unwrap();
if sender.send(Message::Text(json)).await.is_err() {
println!("client disconnected");
return;
}
}
} Removing the How do I allow the client to connect to this socket again, or establish a new websocket on the same port? Any pointers in the right direction? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
I think you have to give the client some id that they send when making another websocket upgrade request, perhaps stored in a cookie. Then you can identify the client and know that you've heard from them before. |
Beta Was this translation helpful? Give feedback.
-
My apologies for my vague question! For the purposes of this question clients have no state but only receive a continuous stream of new information through the web socket. But in my application a refresh of the browser would never reestablish any websocket after the initial one has been disconnected for some reason that I need to explore further. I thought this was inherent to websockets in axum but I was mistaken. In a smaller sample application the websocket does get fully reconnected after a reload of the page (and thus a reload of the handler that upgrades to a websocket). So it appears something else is going on that breaks this in my application. I don't understand what yet, but I'll explore further. Thank you for your help! |
Beta Was this translation helpful? Give feedback.
-
And the use of broadcast instead of mpsc seems to have resolved the problem! |
Beta Was this translation helpful? Give feedback.
And the use of broadcast instead of mpsc seems to have resolved the problem!