1

I want to use the subprotocol with boost websocket.

For example, I have s websocket server address , ws://127.0.0.1:5005. Now I want to replace it with ws://127.0.0.1:5005/order. "order" is the subprotocol in websocket, which could be used in libwebsocket. I find no resource about subprotocol with boost.

vainman
  • 377
  • 1
  • 5
  • 18

2 Answers2

0

handshake method takes a target (the subprotocol as you describe it) as an options parameter.

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
);
selbie
  • 100,020
  • 15
  • 103
  • 173
0

Here is a way to set subprotocol for Websocket in Boost:

if the Boost version >= 1.7.0, then: 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");
}));

else: 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");
});
Dharman
  • 30,962
  • 25
  • 85
  • 135
Mark Cao
  • 156
  • 7