Skip to main content

subprotocols

Function subprotocols 

Source
pub fn subprotocols(req: &HttpRequest) -> impl Iterator<Item = &str>
Expand description

Returns an iterator over the subprotocols requested by the client in the Sec-Websocket-Protocol header.

ยงExample

use ntex::web::{self, HttpRequest, ws};

async fn service(frame: ws::Frame) -> Result<Option<ws::Message>, std::io::Error> {
    // handle incoming frames
    Ok(None)
}

async fn handler(req: HttpRequest) {
    let chosen = ws::subprotocols(&req)
        .find(|p| *p == "my-subprotocol");

    if let Err(err) = ws::start(&req, chosen, service).await {
        eprintln!("WebSocket error: {err:?}");
    }
}

let app = web::App::default().route("/ws", web::get().to(handler));