upgrade_ws

Function upgrade_ws 

Source
pub fn upgrade_ws<H, R, B, E>(
    handler: H,
) -> impl Fn(Request<Body>) -> Ready<Result<Response<B>, E>> + Send + Sync + 'static
where H: Fn(WebSocket) -> R + Copy + 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.

ยงExamples

use routerify_websocket::{upgrade_ws, WebSocket};

async fn ws_handler(ws: WebSocket) {
    println!("New websocket connection: {}", ws.remote_addr());
    // 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(ws_handler))
        .build()
        .unwrap()
}