我如何为 websocket 与 boost beast 握手设置自定义 header

How can I put a custom header for websocket handshake with boost beast

如何在使用 boost beast 的 WebSocket 协议中发生的第一次握手中发送自定义 header?

我想在初始请求中使用自定义 header "x-custome-id : xxxxx"。

你必须把风俗header像这样

您可能想在这里或那里更改一些细节

ws_.async_handshake_ex(host, <endpoint>,
            [<somerequestname>](request_type& reqHead) {
                reqHead.insert(http::field::<sometype>,xxxxx);},
            bind(Some handler));

注:

您将获得 hostendpoint

等详细信息

这就是您想要的 - reqHead.insert(http::field::<sometype>,xxxxx);}http::field::<sometype> 中探索适合您的选项 为此探索 enum class field : unsigned short

我找到了 https://github.com/boostorg/beast/issues/70 的候选解决方案。
但是,这会在 boost 1.69 时发生编译器错误。
此问题也已通过解决方案 https://github.com/boostorg/beast/issues/1470.

解决