【问题标题】:How to connect to websocket server using boost C++如何使用 boost C++ 连接到 websocket 服务器
【发布时间】: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 变量中?那应该是主人的吧?

标签: c++ boost websocket


【解决方案1】:

这是我用来连接到我的网址的示例。我正在从我的客户端发送字符串数据,并使用我的服务器从端口读取它。

如果您能够使用邮递员连接到它,我相信这会起作用。


    auto const address = boost::asio::ip::make_address("127.0.0.1");
    auto const port = static_cast<unsigned short>(std::atoi("9898"));

    boost::asio::io_context ioc { 1 };

    tcp::acceptor acceptor { ioc, { address, port } };
    char againStart;

    while (1) {
        tcp::socket socket { ioc };
        acceptor.accept(socket);
        std::cout << "socket accepted" << std::endl;

        std::thread { [q = std::move(socket)]() mutable {

            boost::beast::websocket::stream<tcp::socket> ws { std::move(q) };

            ws.accept();

            while (1) {
                boost::beast::flat_buffer buffer;

                ws.read(buffer);
            

                auto out = boost::beast::buffers_to_string(buffer.cdata());

                stringVal = out;

                int argument = argument + 1;
                ofstream file;
                file.open("filePath.txt", ios::out | ios::app); //, ios::out | ios::app used to add to another line instead of changing entire file
                file << stringVal;
                file << argument << endl;
                file.close();

            }

        } }.detach();

我还保留了一些我用来从缓冲区读取数据的行。从理论上讲,这应该与您的 url 作为其客户端服务器绑定一起使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-22
    • 2015-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多