move handle close
This commit is contained in:
parent
0e30551221
commit
8c97cee0aa
2 changed files with 80 additions and 73 deletions
|
@ -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<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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<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(())
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue