From 8c97cee0aa7a15b51f986348dc70424013167890 Mon Sep 17 00:00:00 2001 From: Adam <24621027+adoyle0@users.noreply.github.com> Date: Thu, 8 Aug 2024 04:54:02 -0400 Subject: [PATCH] move handle close --- server/src/message_handler.rs | 82 +++++++++++++++++++++++++++++++++-- server/src/websocket.rs | 71 +----------------------------- 2 files changed, 80 insertions(+), 73 deletions(-) diff --git a/server/src/message_handler.rs b/server/src/message_handler.rs index b3fe3c9..283dc6f 100644 --- a/server/src/message_handler.rs +++ b/server/src/message_handler.rs @@ -1,6 +1,6 @@ use crate::user_handler::*; use crate::AppState; -use axum::extract::ws::Message; +use axum::extract::ws::{CloseFrame, Message}; use lib::*; use serde_json::{from_str, to_string}; use std::net::SocketAddr; @@ -44,9 +44,8 @@ impl MessageHandler { _ => tracing::debug!("Unhandled text from {}", addr), }, Message::Binary(data) => tracing::debug!("{} sent binary: {:?}", addr, data), - Message::Close(_close_frame) => { - // make this - // websocket_handle_close(close_frame, &state, tx, addr)?; + Message::Close(close_frame) => { + self.handle_close(close_frame, addr); } Message::Pong(ping) => { tracing::debug!("Pong received with: {:?}", ping); @@ -56,4 +55,79 @@ impl MessageHandler { } } } + + /// This runs when a connection closes + fn handle_close(&self, close_frame: Option, addr: SocketAddr) { + if let Some(cf) = close_frame { + tracing::debug!( + "Close received from {0} with code: {1} and reason: {2}", + self.state + .online_users + .read() + .unwrap() + .get(&addr) + .unwrap() + .read() + .unwrap() + .name, + cf.code, + cf.reason + ) + } else { + tracing::debug!("close received without close frame") + } + + let msg = ChatMessage { + text: format!( + "{0} left.", + self.state + .online_users + .read() + .unwrap() + .get(&addr) + .unwrap() + .read() + .unwrap() + .name + ), + }; + + tracing::debug!("{}", msg.text); + self.state + .broadcast_tx + .send(to_string::(&msg).unwrap()) + .unwrap(); + + // Move user to offline + let name = self + .state + .online_users + .read() + .unwrap() + .get(&addr) + .unwrap() + .read() + .unwrap() + .name + .clone(); + + self.state.offline_users.write().unwrap().insert( + name.clone(), + self.state + .online_users + .write() + .unwrap() + .remove(&addr) + .unwrap(), + ); + + self.state + .broadcast_tx + .send(meta_server_summary_update(&self.state)) + .unwrap(); + self.state + .broadcast_tx + .send(meta_chat_update(&self.state)) + .unwrap(); + } } diff --git a/server/src/websocket.rs b/server/src/websocket.rs index ea61916..f3da435 100644 --- a/server/src/websocket.rs +++ b/server/src/websocket.rs @@ -131,8 +131,8 @@ pub async fn websocket_message_handler( Message::Binary(data) => { tracing::debug!("Binary: {:?}", data) } - Message::Close(close_frame) => { - websocket_handle_close(close_frame, &state, tx, addr)?; + Message::Close(_close_frame) => { + // websocket_handle_close(close_frame, &state, tx, addr)?; } Message::Pong(ping) => { tracing::debug!("Pong received with: {:?}", ping); @@ -177,70 +177,3 @@ fn game_handle_new_game( Ok(()) } - -/// This runs when a connection closes -fn websocket_handle_close( - close_frame: Option, - state: &Arc, - tx: &Sender, - addr: SocketAddr, -) -> Result<()> { - if let Some(cf) = close_frame { - tracing::debug!( - "Close received from {0} with code: {1} and reason: {2}", - state - .online_users - .read() - .unwrap() - .get(&addr) - .unwrap() - .read() - .unwrap() - .name, - cf.code, - cf.reason - ) - } else { - tracing::debug!("close received without close frame") - } - - let msg = ChatMessage { - text: format!( - "{0} left.", - state - .online_users - .read() - .unwrap() - .get(&addr) - .unwrap() - .read() - .unwrap() - .name - ), - }; - - tracing::debug!("{}", msg.text); - tx.send(to_string::(&msg)?)?; - - // Move user to offline - let name = state - .online_users - .read() - .unwrap() - .get(&addr) - .unwrap() - .read() - .unwrap() - .name - .clone(); - - state.offline_users.write().unwrap().insert( - name.clone(), - state.online_users.write().unwrap().remove(&addr).unwrap(), - ); - - tx.send(meta_server_summary_update(state))?; - tx.send(meta_chat_update(state))?; - - Ok(()) -}