Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Sec-WebSocket-Protocol header

BaselineWidely available

The HTTPSec-WebSocket-Protocolrequest andresponse header is used in theWebSocket openinghandshake to negotiate asub-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 theprotocols argument to theWebSocket().The sub-protocol selected by the server is made available to the web application inWebSocket.protocol.

Header typeRequest header,Response header
Forbidden request headerYes (Sec- prefix)

Syntax

http
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 theIANA 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 WebSockethandshake request.The request below shows that the client preferssoap, but also supportswamp.

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

Specifying the protocols like this has the same effect:

http
Sec-WebSocket-Protocol: soapSec-WebSocket-Protocol: wamp

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

http
HTTP/1.1 101 Switching ProtocolsUpgrade: websocketConnection: UpgradeSec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=Sec-WebSocket-Protocol: soap

Specifications

Specification
The WebSocket Protocol
# section-11.3.4

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp