kvarn::websocket

Function response

source
pub async fn response(
    req: &FatRequest,
    host: &Host,
    future: ResponsePipeFuture,
) -> FatResponse
Available on crate feature websocket only.
Expand description

Form a response to a request on a WebSocket route.

Checks the HTTP version, Connection and Upgrade headers, handles the Sec-WebSocket-Accept header, and makes sure this isn’t an unallowed cross origin request.

§Examples

use kvarn::prelude::*;
use kvarn::websocket::{SinkExt, StreamExt};

let mut extensions = Extensions::new();

extensions.add_prepare_single(
    "/ws-ping",
    prepare!(req, host, _path, _addr, {
        kvarn::websocket::response(
            req,
            host,
            response_pipe_fut!(response_pipe, _host, {
                let mut ws = kvarn::websocket::wrap(response_pipe).await.unwrap();
                while let Some(Ok(message)) = ws.next().await {
                    let _ = ws.send(message).await;
                }
            }),
        ).await
    }),
);