Function routerify_ws::upgrade_ws_with_req[][src]

pub fn upgrade_ws_with_req<H, R, B, E>(
    handler: H
) -> impl Fn(Request<Body>) -> Ready<Result<Response<B>, E>> + Send + 'static where
    H: Fn(Request<Body>, WebSocket) -> R + Clone + Send + Sync + 'static,
    R: Future<Output = ()> + Send + 'static,
    B: From<&'static str> + HttpBody + Send + 'static,
    E: Error + Send + 'static, 
Expand description

Upgrades the http requests to websocket while still providing the request for accesing things like headers or extensions.

Examples

use routerify_ws::{upgrade_ws_with_req, WebSocket};

async fn ws_handler(req:Request<Body>,ws: WebSocket) {
    println!("New websocket connection: {} {:?}", ws.remote_addr(),req.headers());
    // Handle websocket connection.
}

fn router() -> Router<Body, Infallible> {
    // Create a router and specify the path and the handler for new websocket connections.
    Router::builder()
        // Upgrade the http requests at `/ws` path to websocket.
        .any_method("/ws", upgrade_ws_with_req(ws_handler))
        .build()
        .unwrap()
}