Skip to content

Instantly share code, notes, and snippets.

@tsidea
Created February 1, 2021 19:03
Show Gist options
  • Save tsidea/711b26602458ed34bd5d6bcecb1da4b2 to your computer and use it in GitHub Desktop.
Save tsidea/711b26602458ed34bd5d6bcecb1da4b2 to your computer and use it in GitHub Desktop.
create websocket hanshake response
use tungstenite::{handshake, Error};
...
match handshake::server::create_response_with_body(&request, || Body::empty()) {
Ok(response) => {
//handle websocket connection HERE
...
//return the response to the handshake request
response
},
Err(error) => {
//probably the handshake request is not up to spec for websocket
println!("Failed to create websocket response \
to request from address {}. \
Error is: {}", remote_addr, error);
let mut res = Response::new(Body::from(format!("Failed to create websocket: {}", error)));
*res.status_mut() = StatusCode::BAD_REQUEST;
return Ok(res);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment