ITADN

WebSocket subprotocol support

#734Closedenisdenjo 创建于 2026-01-31
E
enisdenjocommented
When a client connects specifying a subprotocol using the `Sec-Websocket-Protocol` header in the upgrade request, the server wont match that header in the upgrade response, which [RFC 6455 Section 1.9](https://datatracker.ietf.org/doc/html/rfc6455#section-1.9) describes as the server saying the subprotocol is not supported. When clients (browsers do this automatically) dont receive the matching subprotocol header, they close the connection. Ntex needs to add support for WebSocket subprotocols during the handshake in order to be fully spec compliant. Expanding on the [Websockets documentation](https://ntex.rs/docs/websockets), doing this: ```diff async fn ws_index(req: web::HttpRequest) -> Result<web::HttpResponse, web::Error> { - web::ws::start(req, fn_factory_with_config(ws_service)).await + let mut response = web::ws::start(req, fn_factory_with_config(ws_service)).await?; + response.headers_mut().insert( + HeaderName::from_static("sec-websocket-protocol"), + HeaderValue::from_static("my-subprotocol"), + ); + Ok(response) } ``` doesnt work because internally the `ws::start` already sent out the upgrade response.
关闭于 2026-02-01 3 条评论