大网数据广告活动
Web 标准与前端开发

Sec-WebSocket-Protocol header

· 52CE 我爱测

The HTTP **`Sec-WebSocket-Protocol`** request header and response header is used in the WebSocket opening handshake to negotiate a sub-protocol to use in the communication. This can be a well understood protocol, such as SOAP or WAMP, or a custom protocol understood by the client

The HTTP Sec-WebSocket-Protocol request header and response header is used in the WebSocket opening handshake to negotiate a sub-protocol to use in the communication. This can be a well understood protocol, such as SOAP or WAMP, or a custom protocol understood by the client and server.

In a request the header specifies one or more WebSocket sub-protocols that the web application would like to use, in order of preference. These can be added as protocol values in multiple headers, or as comma separate values added to a single header.

In a response it specifies the sub-protocol selected by the server. This must be the first sub-protocol that the server supports from the list provided in the request header.

The request header is automatically added and populated by the browser using values specified by the application in the protocols argument to the WebSocket(). The sub-protocol selected by the server is made available to the web application in WebSocket.protocol.

<table class="properties"> <tbody> <tr> <th scope="row">Header type</th> <td>Request header, Response header</td> </tr> <tr> <th scope="row">Forbidden request header</th> <td>Yes (<code>Sec-</code> prefix)</td> </tr> </tbody> </table>

Syntax

Sec-WebSocket-Protocol: <sub-protocols>

Directives

  • <sub-protocols>
    • : A comma-separated list of sub-protocol names, in the order of preference. The sub-protocols may be selected from the IANA WebSocket Subprotocol Name Registry, or may be a custom name jointly understood by the client and the server.

      As a response header, this is a single sub-protocol that the server selected.

Examples

WebSocket opening handshake

The sub-protocol is specified in the original WebSocket handshake request. The request below shows that the client prefers soap, but also supports wamp.

GET /chat HTTP/1.1
Host: example.com:8000
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Sec-WebSocket-Version: 13
Sec-WebSocket-Protocol: soap, wamp

Specifying the protocols like this has the same effect:

Sec-WebSocket-Protocol: soap
Sec-WebSocket-Protocol: wamp

The response from the server will include the Sec-WebSocket-Protocol header, selecting the first sub-protocol that it supports from the client's preferences. Below that is shown as soap:

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
Sec-WebSocket-Protocol: soap

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。