Skip to content

Commit

Permalink
server: Do not echo the close message
Browse files Browse the repository at this point in the history
Change the `ws` upgrade to not echo the close message since that is
actually not permitted by `warp`. Instead just close the `Sink` for
a proper clean up.
  • Loading branch information
jsdanielh authored and hrxi committed Feb 27, 2024
1 parent 5949c12 commit 99e0ab4
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,17 @@ impl<D: Dispatcher> Server<D> {
ws.on_upgrade(move |websocket| {
let (mut tx, mut rx) = websocket.split();

let (multiplex_tx, mut multiplex_rx) = mpsc::channel(16); // TODO: What size?
let (multiplex_tx, mut multiplex_rx) = mpsc::channel::<Message>(16); // TODO: What size?

// Forwards multiplexer queue output to websocket
let forward_fut = async move {
while let Some(data) = multiplex_rx.recv().await {
tx.send(data).await?;
// Close the sink if we get a close message (don't echo the message since this is not permitted)
if data.is_close() {
tx.close().await?;
} else {
tx.send(data).await?;
}
}
Ok::<(), Error>(())
};
Expand All @@ -232,16 +237,8 @@ impl<D: Dispatcher> Server<D> {
if message.is_ping() || message.is_pong() {
// Do nothing - these messages are handled automatically
} else if message.is_close() {
// We received the close message, so we need to send a close message and exit the loop
if let Some((code, reason)) = message.close_frame() {
// If the close message contains a code and a reason, we need to echo it back
multiplex_tx
.send(warp::ws::Message::close_with(code, reason.to_owned()))
.await?;
} else {
// Otherwise we echo an empty close message
multiplex_tx.send(warp::ws::Message::close()).await?;
}
// We received the close message, so we need to send a close message to close the sink
multiplex_tx.send(warp::ws::Message::close()).await?;
// Then we exit the loop which closes the connection
break;
} else if let Some(response) = Self::handle_raw_request(
Expand Down

0 comments on commit 99e0ab4

Please sign in to comment.