Skip to content

Commit

Permalink
Ignore connection reset without closing handshake
Browse files Browse the repository at this point in the history
  • Loading branch information
h3ndrk committed Dec 2, 2023
1 parent 9a94513 commit bed5b28
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ time = {version = "0.3.14", features = ["formatting"]}
tokio = {version = "1.26.0", features = ["full"]}
tower-http = {version = "0.5.0", features = ["fs"]}
tower-service = "0.3.2"
tungstenite = "0.20.1"
23 changes: 17 additions & 6 deletions src/presentation/talks_ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use axum::{
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use serde_json::{from_str, to_string};
use tokio::select;
use tungstenite::error::{Error as TungsteniteError, ProtocolError};

use crate::application::{
authentication::{AuthenticationService, Capability, Response},
Expand Down Expand Up @@ -109,12 +110,22 @@ 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 = 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:?}")),
Some(message) => match message {
Ok(Message::Text(message)) => break message,
Ok(Message::Close(_)) => {}
Ok(Message::Ping(_)) => {}
Ok(Message::Pong(_)) => {}
Ok(message) => return Err(format!("expected text message but got {message:?}")),
Err(error) => {
let error = error.into_inner();
if let Some(TungsteniteError::Protocol(
ProtocolError::ResetWithoutClosingHandshake,
)) = error.downcast_ref::<TungsteniteError>()
{
return Ok(None);
}
return Err(error.to_string());
}
},
None => return Ok(None),
}
Expand Down

0 comments on commit bed5b28

Please sign in to comment.