en	Handshaking
zh-hans	握手

en	Client Role
zh-hans	客户端角色

en	A WebSocket session begins when a client sends the HTTP/1.1 [@https://tools.ietf.org/html/rfc7230#section-6.7 Upgrade] request for [@https://tools.ietf.org/html/rfc6455#section-1.3 WebSocket] on an established connection, and the server sends an appropriate response indicating that the request was accepted and that the connection has been upgraded. The Upgrade request must include the [@https://tools.ietf.org/html/rfc7230#section-5.4 Host] field, and the [@https://tools.ietf.org/html/rfc7230#section-5.3 target] of the resource to request. A typical HTTP Upgrade request created and sent by the implementation will look like this:
zh-hans	WebSocket 会话的建立过程如下：客户端在已建立的连接上发送 HTTP/1.1 [@https://tools.ietf.org/html/rfc7230#section-6.7 Upgrade] 请求，服务器则返回相应响应，表示接受该升级请求。该 Upgrade 请求必须包含 [@https://tools.ietf.org/html/rfc7230#section-5.4 Host] 字段以及所请求资源的 [@https://tools.ietf.org/html/rfc7230#section-5.3 目标]。由实现创建并发送的典型 HTTP Upgrade 请求如下所示：

en	WebSocket HTTP Upgrade Request
zh-hans	WebSocket HTTP 升级请求

en	Wire Format
zh-hans	线路格式

en	Description
zh-hans	描述

en	GET / HTTP/1.1 Host: www.example.com Upgrade: websocket Connection: upgrade Sec-WebSocket-Key: 2pGeTR0DsE4dfZs2pH+8MA== Sec-WebSocket-Version: 13 User-Agent: Boost.Beast/216

en	The host and target parameters become part of the Host field and request-target in the resulting HTTP request. The key is generated by the implementation. Callers who wish to add, modify, or inspect fields may set the ['decorator] option on the stream (described later).
zh-hans	主机和目标参数会分别填入所生成的 HTTP 请求中，分别对应 Host 字段和请求目标。密钥由实现自行生成。如果调用方需要添加、修改或检查字段，可以在流上设置修饰器选项（详见下文）。

en	The [link beast.ref.boost__beast__websocket__stream `websocket::stream`] member functions [link beast.ref.boost__beast__websocket__stream.handshake `handshake`] and [link beast.ref.boost__beast__websocket__stream.async_handshake `async_handshake`] are used to send the request with the required host and target strings. This code connects to the IP address returned from a hostname lookup, then performs the WebSocket handshake in the client role.
zh-hans	[link beast.ref.boost__beast__websocket__stream `websocket::stream`] 的成员函数 [link beast.ref.boost__beast__websocket__stream.handshake `handshake`] 和 [link beast.ref.boost__beast__websocket__stream.async_handshake `async_handshake`] 用于发送包含所需主机和目标字符串的请求。以下代码先连接到通过主机名解析获得的 IP 地址，然后以客户端角色执行 WebSocket 握手。

en	When a client receives an HTTP Upgrade response from the server indicating a successful upgrade, the caller may wish to perform additional validation on the received HTTP response message. For example, to check that the response to a basic authentication challenge is valid. To achieve this, overloads of the handshake member function allow the caller to store the received HTTP message in an output reference argument of type [link beast.ref.boost__beast__websocket__response_type `response_type`] as follows:
zh-hans	当客户端收到服务器返回的表示升级成功的 HTTP Upgrade 响应时，调用方可能希望对收到的 HTTP 响应消息进行额外验证。例如，检查基本认证质询的响应是否有效。为此，handshake 成员函数的重载版本允许调用方将收到的 HTTP 消息存储到一个输出引用参数中，该参数类型为 [link beast.ref.boost__beast__websocket__response_type `response_type`]，用法如下：

en	Server Role
zh-hans	服务器角色

en	For servers accepting incoming connections, the [link beast.ref.boost__beast__websocket__stream `websocket::stream`] can read the incoming upgrade request and automatically reply. If the handshake meets the requirements, the stream sends back the upgrade response with a [@https://tools.ietf.org/html/rfc6455#section-4.2.2 ['101 Switching Protocols]] status code. If the handshake does not meet the requirements, or falls outside the range of allowed parameters specified by stream options set previously by the caller, the stream sends back an HTTP response with a status code indicating an error. Depending on the keep alive setting, the connection may remain open for a subsequent handshake attempt. A typical HTTP Upgrade response created and sent by the implementation upon receiving an upgrade request handshake will look like this:
zh-hans	对于接受入站连接的服务器，[link beast.ref.boost__beast__websocket__stream `websocket::stream`] 可以读取入站升级请求并自动回复。如果握手满足要求，流会返回一个状态码为 `101 Switching Protocols` 的升级响应。如果握手不符合要求，或超出调用方先前设置的流选项所允许的参数范围，流会返回一个带有错误状态码的 HTTP 响应。根据 keep-alive 设置，连接可能会保持打开状态，以便后续再次尝试握手。当接收到升级请求握手时，实现创建并发送的典型 HTTP 升级响应如下所示：

en	WebSocket Upgrade HTTP Response
zh-hans	WebSocket 升级 HTTP 响应

en	Wire Format
zh-hans	线路格式

en	Description
zh-hans	描述

en	HTTP/1.1 101 Switching Protocols Upgrade: websocket Connection: upgrade Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo= Server: Boost.Beast

en	The [@https://tools.ietf.org/html/rfc6455#section-11.3.3 ['Sec-WebSocket-Accept]] field value is generated from the request in a fashion specified by the WebSocket protocol.
zh-hans	[@https://tools.ietf.org/html/rfc6455#section-11.3.3 `Sec-WebSocket-Accept`] 字段值按照 WebSocket 协议规定的方式根据请求生成。

en	The [link beast.ref.boost__beast__websocket__stream `stream`] member functions [link beast.ref.boost__beast__websocket__stream.accept `accept`] and [link beast.ref.boost__beast__websocket__stream.async_accept `async_accept`] are used to read the WebSocket HTTP Upgrade request handshake from a stream already connected to an incoming peer, and then send the WebSocket HTTP Upgrade response, as shown:
zh-hans	[link beast.ref.boost__beast__websocket__stream `stream`] 的成员函数 [link beast.ref.boost__beast__websocket__stream.accept `accept`] 和 [link beast.ref.boost__beast__websocket__stream.async_accept `async_accept`] 用于从已连接入站对端的流中读取 WebSocket HTTP 升级请求握手，然后发送 WebSocket HTTP 升级响应，用法如下：

en	Handshake Buffering
zh-hans	握手缓冲机制

en	It is possible for servers to read data from the stream and decide later that the buffered bytes should be interpreted as a WebSocket upgrade request. To address this usage, overloads of [link beast.ref.boost__beast__websocket__stream.accept `accept`] and [link beast.ref.boost__beast__websocket__stream.async_accept `async_accept`] which accept an additional buffer sequence parameter are provided.
zh-hans	服务器可能会先读取流中的数据，稍后才决定将这些已缓冲的字节作为 WebSocket 升级请求来处理。为此，该库提供了 [link beast.ref.boost__beast__websocket__stream.accept `accept`] 和 [link beast.ref.boost__beast__websocket__stream.async_accept `async_accept`] 的重载版本，它们接受一个额外的缓冲区序列参数。

en	In this example, the server reads the initial HTTP request header into a dynamic buffer, then later uses the buffered data to attempt a websocket upgrade.
zh-hans	在本示例中，服务器先将初始 HTTP 请求头部读入动态缓冲区，随后使用缓冲数据尝试进行 WebSocket 升级。

en	Inspecting HTTP Requests
zh-hans	检查 HTTP 请求内容

en	When implementing an HTTP server that also supports WebSocket, the server usually reads the HTTP request from the client. To detect when the incoming HTTP request is a WebSocket Upgrade request, the function [link beast.ref.boost__beast__websocket__is_upgrade `is_upgrade`] may be used.
zh-hans	当实现同时支持 WebSocket 的 HTTP 服务器时，服务器通常需要先读取客户端发来的 HTTP 请求。若要判断该请求是否为 WebSocket 升级请求，可以使用 [link beast.ref.boost__beast__websocket__is_upgrade `is_upgrade`] 函数。

en	Once the caller determines that the HTTP request is a WebSocket Upgrade, additional overloads of [link beast.ref.boost__beast__websocket__stream.accept `accept`] and [link beast.ref.boost__beast__websocket__stream.async_accept `async_accept`] are provided which receive the entire HTTP request header as an object to perform the handshake. By reading the request manually, the program can handle normal HTTP requests as well as upgrades. The program may also enforce policies based on the HTTP fields, such as Basic Authentication. In this example, the request is first read in using the HTTP algorithms, and then passed to a newly constructed stream:
zh-hans	一旦确认 HTTP 请求是 WebSocket 升级请求，就可以使用 [link beast.ref.boost__beast__websocket__stream.accept `accept`] 和 [link beast.ref.boost__beast__websocket__stream.async_accept `async_accept`] 的额外重载版本，这些重载接受整个 HTTP 请求头部对象来完成握手。通过手动读取请求，程序既能处理普通 HTTP 请求，也能处理升级请求。同时，程序还可以基于 HTTP 字段（例如基本认证）来实施策略控制。以下示例先使用 HTTP 算法读取请求，然后将其传递给新构造的流：

en	Subprotocols
zh-hans	子协议

en	The WebSocket protocol understands the concept of subprotocols. If the client is requesting one of a set of subprotocols it will set the header [@https://tools.ietf.org/html/rfc6455#section-11.3.4 Sec-WebSocket-Protocol] in the initial WebSocket Upgrade HTTP request. It is up to the server to parse the header and select one of the protocols to accept. The server indicates the selected protocol by setting the [@https://tools.ietf.org/html/rfc6455#section-11.3.4 Sec-WebSocket-Protocol] header in the accept header.
zh-hans	WebSocket 协议支持子协议的概念。如果客户端请求使用某个子协议，它会在初始 WebSocket 升级 HTTP 请求中设置 [@https://tools.ietf.org/html/rfc6455#section-11.3.4 Sec-WebSocket-Protocol] 头部。服务器需要解析该头部，并从中选择一个协议接受。服务器通过在响应头部中设置 [@https://tools.ietf.org/html/rfc6455#section-11.3.4 Sec-WebSocket-Protocol] 头部来指示所选协议。

en	This is accomplished with a [link beast.ref.boost__beast__websocket__stream_base__decorator `decorator`].
zh-hans	这可以通过 [link beast.ref.boost__beast__websocket__stream_base__decorator `decorator`] 来实现。

en	The code that follows demonstrates how a server reads an HTTP request, identifies it as a WebSocket Upgrade, and then checks for a preferred matching subprotocol before performing the WebSocket handshake:
zh-hans	以下代码演示服务器如何读取 HTTP 请求，识别其为 WebSocket 升级请求，并在执行 WebSocket 握手之前检查是否存在匹配的优先子协议：