pub fn ws<ReqB, B, T, E>(
req: &Request<ReqB>,
body: B,
) -> Result<WsOutput<B>, HandshakeError>Expand description
A shortcut for generating a set of response types with given Request and <Body> type.
<Body> must be a type impl futures_core::Stream trait with Result<T: AsRef<[u8]>, E>
as Stream::Item associated type.
ยงExamples:
use http_ws::{ws, Message};
// an incoming http request.
let mut req = Request::get("/")
.header(header::UPGRADE, header::HeaderValue::from_static("websocket"))
.header(header::CONNECTION, header::HeaderValue::from_static("upgrade"))
.header(header::SEC_WEBSOCKET_VERSION, header::HeaderValue::from_static("13"))
.header(header::SEC_WEBSOCKET_KEY, header::HeaderValue::from_static("some_key"))
.body(())
.unwrap();
// http request body associated with http request.
let body = DummyRequestBody;
// generate response from request and it's body.
let (mut req_stream, response, res_stream) = ws(&mut req, DummyRequestBody).unwrap();
// req_stream must be polled with Stream interface to receive websocket message
use futures_util::stream::StreamExt;
if let Some(Ok(msg)) = req_stream.next().await {
// res_stream can be used to send websocket message to client.
res_stream.send(msg).await.unwrap();
}