Skip to content

Commit

Permalink
Fix read busy loop
Browse files Browse the repository at this point in the history
  • Loading branch information
h3ndrk committed Dec 2, 2023
1 parent 50fd72b commit c8f86e9
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions src/presentation/talks_ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ async fn talks_ws_connection(
.trigger(user_id, &capabilities, command)
.await
.map_err(|error| error.to_string())?,
None => {},
None => break
}
},
update = updates.recv() => {
Expand All @@ -95,6 +95,8 @@ async fn talks_ws_connection(
},
}
}

Ok(())
}

async fn send(socket: &mut WebSocket, message: &impl Serialize) -> Result<(), String> {
Expand All @@ -105,16 +107,17 @@ async fn send(socket: &mut WebSocket, message: &impl Serialize) -> Result<(), St
}

async fn receive<T: DeserializeOwned>(socket: &mut WebSocket) -> Result<Option<T>, String> {
let message = match socket.recv().await {
Some(message) => message,
None => return Ok(None),
};
let message = match message.map_err(|error| error.to_string())? {
Message::Text(message) => message,
Message::Close(_) => return Ok(None),
Message::Ping(_) => return Ok(None),
Message::Pong(_) => return Ok(None),
message => return Err(format!("expected text message but got {message:?}")),
let message = loop {
match socket.recv().await {
Some(message) => match message.map_err(|error| error.to_string())? {
Message::Text(message) => break message,
Message::Close(_) => {}
Message::Ping(_) => {}
Message::Pong(_) => {}
message => return Err(format!("expected text message but got {message:?}")),
},
None => return Ok(None),
}
};
from_str(&message)
.map(|message| Some(message))
Expand Down

0 comments on commit c8f86e9

Please sign in to comment.