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

Sec-WebSocket-Version header

· 52CE 我爱测

The HTTP **Sec-WebSocket-Version** request header and response header is used in the WebSocket opening handshake to indicate the WebSocket protocol supported by the client, and the protocol versions supported by the server if it does _not_ support the version specified in the req

The HTTP Sec-WebSocket-Version request header and response header is used in the WebSocket opening handshake to indicate the WebSocket protocol supported by the client, and the protocol versions supported by the server if it does not support the version specified in the request.

The header can only appear once in a request, and specifies the WebSocket version that web application is using. The current version of the protocol at time of writing is 13. The header is automatically added to requests by user agents when a WebSocket connection is established.

The server uses the version to determine if it can understand the protocol. If the server doesn't support the version, or any header in the handshake is not understood or has an incorrect value, the server should send a response with status 400 and immediately close the socket. It should also include Sec-WebSocket-Version in the 400 response, listing the versions that it does support. The versions can be specified in individual headers, or as comma-separate values in a single header.

The header should not be sent in responses if the server understands the version specified by the client.

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

Syntax

Request

Sec-WebSocket-Version: <version>

Response (on error only):

Sec-WebSocket-Version: <server-supported-versions>

Directives

  • <version>
    • : The WebSocket protocol version the client wishes to use when communicating with the server. This number should be the most recent version possible listed in the IANA WebSocket Version Number Registry. The most recent final version of the WebSocket protocol is version 13.
  • <server-supported-versions>
    • : On error, a comma-delineated list of the WebSocket protocol versions supported by the server. The header is not sent in responses if <version> is supported.

Examples

WebSocket opening handshake

The version supported by the client is specified in the original WebSocket handshake request. For the current protocol, the version is "13", as shown below.

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

If the server supports version 13 of the protocol, then Sec-WebSocket-Version will not appear in the response.

Specifications

Browser compatibility

See also

  • Sec-WebSocket-Accept
  • Sec-WebSocket-Key
  • Sec-WebSocket-Protocol
  • Sec-WebSocket-Extensions
  • The WebSocket handshake in Writing WebSocket servers

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

资料来源与适用范围

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

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

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