boost::asio中websocket的相对路径

问题描述 投票:0回答:1

我已经知道boost::asio的基本用法如:

    std::size_t CacheSize = 1024;
    boost::asio::io_context global_io_context;
    boost::asio::ip::tcp::socket cur_socket(global_io_context);
    boost::asio::ip::tcp::resolver cur_resolver(global_io_context);
    boost::asio::ip::tcp::resolver::results_type cur_endpoints;
    std::vector<std::uint8_t> Cache;
    try
    {
        Cache.resize(CacheSize);
        boost::asio::ip::tcp::resolver::query query("127.0.0.1", "82");
        if (cur_endpoints.size() <= 0)
        {
            cur_endpoints = cur_resolver.resolve(query);
        }
        boost::asio::connect(cur_socket, cur_endpoints);
        boost::asio::read(cur_socket, boost::asio::buffer(Cache));
    }
    catch (std::exception& ex)
    {
        std::cout << ex.what() << std::endl;
    }

它适用于网址“ws://127.0.0.1:82”。

但我需要的是使用“ws://127.0.0.1:82/rav_path” 我尝试了上面的代码:连接成功,但是收不到任何数据!

在哪里输入“/rav_path”?或者是否有任何类型的查询可以直接设置整个 url,而不是分别设置主机、服务和 rav_path?就像 C# 中的 ClientWebSocket:

web = new ClientWebSocket();
var w = web.ConnectAsync(new System.Uri("ws://127.0.0.1:82/rav_path"), CancellationToken.None);
c++ boost-asio
1个回答
0
投票

像这样使用 boost::beast:

...
boost::beast::flat_buffer buffer;
...
boost::asio::connect(cur_socket.next_layer(), cur_endpoints);
// cur_socket.set_option( ... ) // Optional. see: [Simple WebSocket Client][1]

// The HTTP handshaking process mentioned by Alan Birtles( Thank you :)  )
cur_socket.handshake("127.0.0.1:82", "/rav_path"); // The relative path is applied here.

cur_socket.read(buffer);
...
© www.soinside.com 2019 - 2024. All rights reserved.