From af116794c427c44486b4277652995c313d3367f1 Mon Sep 17 00:00:00 2001 From: J H <2364004+Blu-J@users.noreply.github.com> Date: Wed, 16 Aug 2023 12:43:17 -0600 Subject: [PATCH] =?UTF-8?q?fix:=20Add=20in=20the=20code=20to=20create=20th?= =?UTF-8?q?e=20life=20check=20for=20the=20ui=20to=20keep=20the=20=E2=80=A6?= =?UTF-8?q?=20(#2391)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: Add in the code to create the life check for the ui to keep the ws alive * Update Cargo.toml * Update rpc.rs --- backend/src/db/mod.rs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/backend/src/db/mod.rs b/backend/src/db/mod.rs index a8cc11d1f..54b849610 100644 --- a/backend/src/db/mod.rs +++ b/backend/src/db/mod.rs @@ -14,6 +14,7 @@ use rpc_toolkit::hyper::{Body, Error as HyperError, Request, Response}; use rpc_toolkit::yajrc::RpcError; use serde::{Deserialize, Serialize}; use serde_json::Value; +use tokio::io::AsyncWrite; use tokio::sync::oneshot; use tokio::task::JoinError; use tokio_tungstenite::tungstenite::protocol::frame::coding::CloseCode; @@ -82,6 +83,7 @@ async fn deal_with_messages( mut sub: patch_db::Subscriber, mut stream: WebSocketStream, ) -> Result<(), Error> { + let mut timer = tokio::time::interval(tokio::time::Duration::from_secs(5)); loop { futures::select! { _ = (&mut kill).fuse() => { @@ -104,14 +106,19 @@ async fn deal_with_messages( } message = stream.next().fuse() => { let message = message.transpose().with_kind(crate::ErrorKind::Network)?; - match message { - None => { - tracing::info!("Closing WebSocket: Stream Finished"); - return Ok(()) - } - _ => (), + if message.is_none() { + tracing::info!("Closing WebSocket: Stream Finished"); + return Ok(()) } } + // This is trying to give a health checks to the home to keep the ui alive. + _ = timer.tick().fuse() => { + stream + .send(Message::Ping(vec![])) + .await + .with_kind(crate::ErrorKind::Network)?; + } + } } }