From e259a6768336a693a62c88b894ff777a6e4fd55a Mon Sep 17 00:00:00 2001 From: c0repwn3r Date: Wed, 12 Apr 2023 08:33:01 -0400 Subject: [PATCH] fix the client ping system --- client/src/lib.rs | 24 ++++++++++++++++-------- server/src/handler.rs | 8 ++++---- web/play.html | 14 +++++++++++++- 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/client/src/lib.rs b/client/src/lib.rs index 8f6e234c73a0a5c8d68f7b01cc73c51dd34bcf48..24055ec2a67c395a4cf5bd15b7a4698d50662b3e 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -181,8 +181,24 @@ pub async fn main(gateway: &str, username: &str, backoff: i32, textures: Texture Ok(()) } +#[wasm_bindgen] +pub async fn send_ping_pong() -> Result<(), JsError> { + let mut client = CLIENT.write()?; + + if client.client_data.is_none() { + return Err(JsError::new("Client not yet initialized")); + } + + let client_data = client.client_data.as_mut().unwrap(); + + send!(client_data.tx, &MessageC2S::Ping {}).await?; + + Ok(()) +} + #[wasm_bindgen] pub async fn update_socket() -> Result<(), JsError> { + let mut client = CLIENT.write()?; if client.client_data.is_none() { @@ -201,11 +217,6 @@ pub async fn update_socket() -> Result<(), JsError> { return Err(JsError::new("Connection timed out")); } - if client_data.pong_timeout - 3 < (js_sys::Date::now() as u64 / 1000) { - // send ping - send!(client_data.tx, &MessageC2S::Ping {}).await?; - } - let maybe_msg: Option = match recv!(client_data.rx) { Ok(r) => r, Err(e) => { @@ -236,17 +247,14 @@ pub async fn update_socket() -> Result<(), JsError> { chatbox.append_child(&new_elem).unwrap(); }, MessageS2C::Pong {} => { - info!("this is a pong response"); client_data.pong_timeout = (js_sys::Date::now() as u64 / 1000) + PONG_MAX_TIMEOUT }, MessageS2C::PlanetData { planets } => { - debug!("updated planet information {:?}", planets); client.planets = planets; }, MessageS2C::Position { x, y } => { client.x = x; client.y = y; - debug!("updated position information {}, {}", x, y); } _ => { warn!("server sent unexpected packet {:?}, ignoring", msg); diff --git a/server/src/handler.rs b/server/src/handler.rs index 868b0110a9e91e571baaa7c7ed91b7c412317ed8..4c539bb3803b9d6cff7c5a67100a33aeffc5e7e5 100644 --- a/server/src/handler.rs +++ b/server/src/handler.rs @@ -20,10 +20,10 @@ use crate::{send, recv, SCALE}; pub async fn handle_client(mgr: ClientManager, data: Arc>, remote_addr: SocketAddr, mut rx: Receiver, mut client_tx: SplitSink, Message>, mut client_rx: SplitStream>) -> Result<(), Box> { let mut state = State::Handshake; let mut username = String::new(); - let mut ping_timeout = SystemTime::now() + Duration::from_secs(5); + let mut ping_timeout = SystemTime::now() + Duration::from_secs(10); loop { - info!("{}", ping_timeout.duration_since(UNIX_EPOCH).unwrap().as_secs() - SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs()); + if let Some(msg) = rx.recv().await { match msg { ClientHandlerMessage::Tick => {} // this intentionally does nothing, @@ -51,7 +51,7 @@ pub async fn handle_client(mgr: ClientManager, data: Arc>, r } if ping_timeout < SystemTime::now() { - warn!("[{}] ping timeout", remote_addr); + error!("[{}] ping timeout", remote_addr); send!(client_tx, &MessageS2C::Goodbye { reason: PingPongTimeout }).await?; @@ -168,7 +168,7 @@ pub async fn handle_client(mgr: ClientManager, data: Arc>, r }, MessageC2S::Ping {} => { send!(client_tx, &MessageS2C::Pong {}).await?; - ping_timeout = SystemTime::now() + Duration::from_secs(5); + ping_timeout = SystemTime::now() + Duration::from_secs(10); } } } diff --git a/web/play.html b/web/play.html index 4aef66765d1a2d6be7560a542d2c506233632340..641e5d32b88c77a1bf36e7b38cf9a56a0861b09d 100644 --- a/web/play.html +++ b/web/play.html @@ -25,7 +25,7 @@