[/
    Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)

    Distributed under the Boost Software License, Version 1.0. (See accompanying
    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

    Official repository: https://github.com/boostorg/beast
]

[/-----------------------------------------------------------------------------]

[section:timeouts 超时]

尽管 [link beast.ref.boost__beast__basic_stream `basic_stream`] 和 [link beast.ref.boost__beast__basic_stream `tcp_stream`] 支持对常规逻辑操作设置超时，但 WebSocket 流内置了更复杂的超时机制，可以启用并进行配置。在使用 WebSocket 流时，不应使用 TCP 或基础流的超时功能。这些超时功能的接口如下表所示。

[table WebSocket 超时接口
[[名称][描述]]
[[
    [link beast.ref.boost__beast__websocket__stream_base__timeout `stream_base::timeout`]
][该结构表示 WebSocket 流的超时配置设置。
]]
[[
    [link beast.ref.boost__beast__websocket__stream_base__timeout.suggested `stream_base::timeout::suggested`]
][该函数返回给定角色（客户端或服务器）的建议超时设置。
]]
[[
    [link beast.ref.boost__beast__websocket__stream.set_option `stream::set_option`]
][该函数用于在流上设置超时及其他选项。
]]
]

流上可以独立设置三种超时配置：

[table WebSocket 超时接口（2）
[[名称][类型][描述]]
[
    [[link beast.ref.boost__beast__websocket__stream_base__timeout.handshake_timeout `timeout::handshake_timeout`]]
    [`duration`（超时时间）]
    [该值表示握手超时的时间长度。该超时适用于客户端握手、服务器握手，以及连接任一端希望关闭时执行的 WebSocket 关闭握手。可将 [link beast.ref.boost__beast__websocket__stream_base.none `stream_base::none()`] 的返回值赋给该值以禁用此超时。]
][
    [[link beast.ref.boost__beast__websocket__stream_base__timeout.idle_timeout `timeout::idle_timeout`]]
    [`duration`（超时时间）]
    [如果在对端没有收到数据或控制帧的时间达到空闲超时设定值，则连接将触发超时。可将 [link beast.ref.boost__beast__websocket__stream_base.none `stream_base::none()`] 的返回值赋给该值以禁用此超时。]
][
    [[link beast.ref.boost__beast__websocket__stream_base__timeout.keep_alive_pings `timeout::keep_alive_pings`]]
    [`bool`（布尔值）]
    [如果空闲超时启用，则该设置控制当在空闲超时时间的一半内未收到数据时，是否向对端发送 ping 帧。]
]
]

默认情况下，WebSocket 流的超时功能处于禁用状态。启用该功能最简单的方式是在流上设置建议的超时配置。

[code_websocket_6_1]

若需手动控制超时设置，可以构造一个超时选项对象。以下示例用于仅启用手动超时：

[code_websocket_6_2]

超时通知会通过调用未完成的异步读操作的完成处理器传达给调用方，同时附带错误码 [link beast.ref.boost__beast__error `error::timeout`]。在传递此错误之前，实现会关闭套接字或流，因此无需手动关闭连接。要收到该错误，必须存在一个未完成的读操作。

[code_websocket_6_3]

[note
    WebSocket 超时功能仅在异步 I/O 模式下可用。
]

WebSocket 流的超时机制与 tcp_stream 的超时机制不兼容。当从已启用超时的 tcp 流构造 WebSocket 流时，应首先禁用 tcp 流的超时，如下所示。

[code_websocket_6_4]


[endsect]
