【发布时间】:2021-09-11 01:02:16
【问题描述】:
我的 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, "/");
但我无法连接到服务器。 如何设置路径“/webstream/wsocket”进行连接。 '''
我尝试过的:
i tried with
_host = "localhost/webstream/wsocket";
_port = 80;
_pws->handshake(host, "/webstream/wsocket");
但未连接 boost库中如何指定websocket服务器的路径
【问题讨论】:
-
localhost/webstream/wsocket不是主机,所以这可能行不通。请问,您能否确保您的代码符合minimal reproducible example 准则?有太多的猜测。 -
使用邮递员我能够连接到 websocket 服务器 (ws://localhost/webstream/wsocket) 但我的问题是我无法在 boost 库中找到将连接到路径的函数。我尝试使用另一个 websocket 服务器 ws://localhost:5000 ==> 这在此代码中工作正常但如果它包含子路径..example ws://localhost:5000/wssocket 此代码不起作用
-
为什么要将路径放在
_host变量中?那应该是主人的吧?