From 180e7ec4ce0e6fb33ec7da4515e2a14b32294460 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Tue, 11 Aug 2026 21:02:26 -0400 Subject: [PATCH] read loop --- src/main.rs | 11 +++++++++-- src/protocol/mod.rs | 24 ++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 0b6f08c..df8bf2c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,9 +22,16 @@ async fn main() { async fn ws_handler(ws: WebSocketUpgrade) -> Response { ws.on_upgrade(|socket: WebSocket| async { match Client::initialize(socket).await { - Ok(client) => {} + Ok(mut client) => { + if let Err(e) = client.read_loop().await { + eprintln!("Failed to handle client: {e}"); + } else { + println!("Client connection closed") + } + } + Err(e) => { - eprintln!("Failed to initialize client {e}") + eprintln!("Failed to initialize client: {e}") } } }) diff --git a/src/protocol/mod.rs b/src/protocol/mod.rs index 47e4e90..1bd8dfa 100644 --- a/src/protocol/mod.rs +++ b/src/protocol/mod.rs @@ -23,8 +23,9 @@ pub enum ClientMethod { signature: String, }, - #[serde(rename = "error")] - Error { error: Cow<'static, str> }, + Error { + error: Cow<'static, str>, + }, } #[derive(Debug, Clone, Serialize, Deserialize)] @@ -42,6 +43,25 @@ pub enum ServerMethod { } impl Client { + pub async fn read_loop(&mut self) -> anyhow::Result<()> { + while let Some(message) = self.read().await? { + match message { + ServerMethod::Initialize { .. } => { + self.send(ClientMethod::Error { + error: Cow::Borrowed("Already initialized"), + }) + .await?; + } + + ServerMethod::Error { error } => { + eprintln!("Client error: {error}"); + } + } + } + + Ok(()) + } + pub async fn read_socket(socket: &mut WebSocket) -> anyhow::Result> { match socket.recv().await.transpose()? { Some(Message::Text(text)) => {