如何使用Boost WebOScket设置子协议

时间:2019-05-25 03:45:10

标签: websocket beast-websockets

我想将子协议与boost websocket一起使用。

例如,我有一个websocket服务器地址,ws://127.0.0.1:5005。 现在我想用ws://127.0.0.1:5005 / order替换它。 “ order”是websocket中的子协议,可以在libwebsocket中使用。 我找不到有关带有Boost的子协议的资源。

2 个答案:

答案 0 :(得分:0)

handshake方法将目标(如您所描述的子协议)作为选项参数。

Per documentation

// Do the websocket handshake in the client role, on the connected stream.
// The implementation only uses the Host parameter to set the HTTP "Host" field,
// it does not perform any DNS lookup. That must be done first, as shown above.

ws.handshake(
    "www.example.com",  // The Host field
    "/order"            // The request-target
);

答案 1 :(得分:0)

这是在Boost中为Websocket设置子协议的方法:

如果Boost版本> = 1.7.0,则:Click here for more detail

stream<tcp_stream> ws(ioc);
ws.set_option(stream_base::decorator(
[](request_type& req)
{
    // Set the client field on the request
    req.set(boost::beast::http::field::sec_websocket_protocol, "protoo");
    req.set(boost::beast::http::field::sec_websocket_version, "13");
    req.set(boost::beast::http::field::sec_websocket_extensions,
            "xxx");
}));

其他:Click here for more detail

stream<tcp_stream> ws(ioc);
ws.handshake_ex("ws://127.0.0.1:5005", "/",
[](request_type& req)
{
    req.insert(boost::beast::http::field::sec_websocket_protocol, "protoo");
    req.insert(boost::beast::http::field::sec_websocket_version, "13");
    req.insert(boost::beast::http::field::sec_websocket_extensions,
            "xxx");
});
相关问题