diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f9463ba..3267db6e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ### Removed ### Changed ### Fixed +- The `trycp_client` now handles ping/pong messages from the TryCP server to keep the connection alive. +- The `trycp_client` now handles the `close` event from the TryCP server to close the connection. ## 2024-06-13: v0.17.0-dev.1 ### Added diff --git a/crates/trycp_client/src/lib.rs b/crates/trycp_client/src/lib.rs index 68a6ec86..53344423 100644 --- a/crates/trycp_client/src/lib.rs +++ b/crates/trycp_client/src/lib.rs @@ -72,10 +72,30 @@ impl TrycpClient { let (recv_send, recv_recv) = tokio::sync::mpsc::channel(32); + let ws = Arc::new(tokio::sync::Mutex::new(sink)); + let ws2 = ws.clone(); let pend2 = pend.clone(); let recv_task = tokio::task::spawn(async move { while let Some(Ok(msg)) = stream.next().await { - let msg = msg.into_data(); + let msg = match msg { + Message::Close(close_msg) => { + eprintln!("Received websocket close from TryCP server: {}", close_msg.map(|f| f.reason).unwrap_or("No reason".into())); + break; + } + Message::Ping(p) => { + ws2.lock().await.send(Message::Pong(p)).await.unwrap(); + continue; + } + Message::Pong(_) => { + continue; + } + Message::Binary(msg) => { + msg + } + _ => { + panic!("Unexpected message from TryCP server: {:?}", msg); + } + }; let msg: MessageToClient = rmp_serde::from_slice(&msg).unwrap(); match msg { @@ -91,8 +111,6 @@ impl TrycpClient { } }); - let ws = Arc::new(tokio::sync::Mutex::new(sink)); - Ok(( Self { ws, diff --git a/crates/trycp_server/src/app_interface.rs b/crates/trycp_server/src/app_interface.rs index 79079f09..3e971c23 100644 --- a/crates/trycp_server/src/app_interface.rs +++ b/crates/trycp_server/src/app_interface.rs @@ -61,7 +61,7 @@ pub(crate) async fn connect( port: u16, response_writer: Arc>, ) -> Result<(), ConnectError> { - let connection_lock = Arc::clone(&CONNECTIONS.lock().await.entry(port).or_default()); + let connection_lock = Arc::clone(CONNECTIONS.lock().await.entry(port).or_default()); let mut connection = connection_lock.lock().await; if connection.is_some() { diff --git a/crates/trycp_server/src/download_dna.rs b/crates/trycp_server/src/download_dna.rs index 209cd475..fe91d726 100644 --- a/crates/trycp_server/src/download_dna.rs +++ b/crates/trycp_server/src/download_dna.rs @@ -74,7 +74,7 @@ pub(crate) async fn download_dna(url_str: String) -> Result PathBuf { Path::new(DNA_DIR_PATH) .join(url.scheme()) - .join(url.path().replace("/", "").replace("%", "_")) + .join(url.path().replace('/', "").replace('%', "_")) } /// Tries to create a file, returning Ok(None) if a file already exists at path