如何使用 boost C++ 连接到 websocket 服务器

Dil*_* ND 5 c++ boost websocket

我的 websocket 服务器 URL 是ws://localhost/webstream/wsocket 我正在尝试创建一个使用 boost 连接到此服务器的 C++ websocket 客户端

tcp::resolver resolver{ioc};
_pws = new websocket::stream<tcp::socket>(ioc);

// Look up the domain name
// my server is http://localhost/webstream/wsocket
_host = "localhost";
_port = 80;

auto const results = resolver.resolve(_host, std::to_string(_port));
if(results.size() == 0)
{
    std::cout<<"failed to connect to websocket server"<<std::endl;
    delete _pws;
    _pws = nullptr;
    return;
}

// Make the connection on the IP address we get from a lookup
net::connect(_pws->next_layer(), results.begin(), results.end());

// Set a decorator to change the User-Agent of the handshake
_pws->set_option(websocket::stream_base::decorator(
    [](websocket::request_type& req)
    {
        req.set(http::field::user_agent,
            std::string(BOOST_BEAST_VERSION_STRING) +
                " websocket-client-coro");
    }));
_pws->handshake(host, "/");
Run Code Online (Sandbox Code Playgroud)

但我无法连接到服务器。我如何设置路径“/webstream/wsocket”进行连接。'''

我尝试过的:

i tried with
_host = "localhost/webstream/wsocket";
_port = 80;

_pws->handshake(host, "/webstream/wsocket");
Run Code Online (Sandbox Code Playgroud)

但无法连接 在boost库中,我们如何指定websocket服务器的路径

小智 0

类似这个问题

访问websocket url的正确方式:ws://localhost:5000/webstream/wsocket,需要设置

 host_ = "localhost";
 port_ = 5000;
Run Code Online (Sandbox Code Playgroud)

解析端点并连接后,握手/webstream/wsocket时需要路径

_pws->handshake(host, "/webstream/wsocket")
Run Code Online (Sandbox Code Playgroud)

这样就可以成功建立websocket连接了