move handle close

This commit is contained in:
Adam 2024-08-08 04:54:02 -04:00
parent 0e30551221
commit 8c97cee0aa
2 changed files with 80 additions and 73 deletions

View file

@ -1,6 +1,6 @@
use crate::user_handler::*; use crate::user_handler::*;
use crate::AppState; use crate::AppState;
use axum::extract::ws::Message; use axum::extract::ws::{CloseFrame, Message};
use lib::*; use lib::*;
use serde_json::{from_str, to_string}; use serde_json::{from_str, to_string};
use std::net::SocketAddr; use std::net::SocketAddr;
@ -44,9 +44,8 @@ impl MessageHandler {
_ => tracing::debug!("Unhandled text from {}", addr), _ => tracing::debug!("Unhandled text from {}", addr),
}, },
Message::Binary(data) => tracing::debug!("{} sent binary: {:?}", addr, data), Message::Binary(data) => tracing::debug!("{} sent binary: {:?}", addr, data),
Message::Close(_close_frame) => { Message::Close(close_frame) => {
// make this self.handle_close(close_frame, addr);
// websocket_handle_close(close_frame, &state, tx, addr)?;
} }
Message::Pong(ping) => { Message::Pong(ping) => {
tracing::debug!("Pong received with: {:?}", 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<CloseFrame>, 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::<ChatMessage>(&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();
}
} }

View file

@ -131,8 +131,8 @@ pub async fn websocket_message_handler(
Message::Binary(data) => { Message::Binary(data) => {
tracing::debug!("Binary: {:?}", data) tracing::debug!("Binary: {:?}", data)
} }
Message::Close(close_frame) => { Message::Close(_close_frame) => {
websocket_handle_close(close_frame, &state, tx, addr)?; // websocket_handle_close(close_frame, &state, tx, addr)?;
} }
Message::Pong(ping) => { Message::Pong(ping) => {
tracing::debug!("Pong received with: {:?}", ping); tracing::debug!("Pong received with: {:?}", ping);
@ -177,70 +177,3 @@ fn game_handle_new_game(
Ok(()) Ok(())
} }
/// This runs when a connection closes
fn websocket_handle_close(
close_frame: Option<CloseFrame>,
state: &Arc<AppState>,
tx: &Sender<String>,
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::<ChatMessage>(&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(())
}