【问题标题】:How to use Boost Web Socket client for sending multiple headers?如何使用 Boost Web Socket 客户端发送多个标头?
【发布时间】:2021-12-29 11:50:04
【问题描述】:

我想向我拥有的 WebSocket 服务器发送 3-4 个标头,标头是 action = subscribe,userID = <some email address>,agentID =831C5DFC-1643-40C4-A5A3-9C918556D3A1 ,我无法理解如何将这些标头发送到服务器,例如是典型的方法吗?这是我的客户代码?????????

#include <boost/beast/http.hpp>
#include <boost/asio/connect.hpp>
#include <boost/beast/core.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/asio/connect.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/beast/websocket/stream.hpp>
#include <cstdlib>
#include <iostream>
#include <string>

namespace http = boost::beast::http;
namespace beast = boost::beast;         // from <boost/beast.hpp>
namespace http = beast::http;           // from <boost/beast/http.hpp>
namespace websocket = beast::websocket; // from <boost/beast/websocket.hpp>
namespace net = boost::asio;            // from <boost/asio.hpp>
using tcp = boost::asio::ip::tcp;       // from <boost/asio/ip/tcp.hpp>

// Sends a WebSocket message and prints the response
#define SERVER_URL "127.0.0.1"
#define SERVER_PORT "80"


class set_subprotocols
{
    std::string s_;

public:
    explicit
    set_subprotocols(std::string s)
        : s_(s) {}

    template<bool isRequest, class Body, class Headers>
    void
    operator()(boost::beast::http::message<isRequest, Body, Headers>& m) const
    {
        m.set("X-Custome-Id", s_);
    }
};


int main(int argc, char** argv)
{
    try
    {
        // Check command line arguments.
        
        auto const host = SERVER_URL;
        auto const port = SERVER_PORT;
        auto const text = "hello world";

        // The io_context is required for all I/O
        net::io_context ioc;

        // These objects perform our I/O
        tcp::resolver resolver{ioc};
        websocket::stream<tcp::socket> ws{ioc};
        // Look up the domain name
        auto const results = resolver.resolve(host, port,boost::asio::ip::resolver_query_base::numeric_service);

        // Make the connection on the IP address we get from a lookup
        
        net::connect(ws.next_layer(), results.begin(), results.end());
        // Set a decorator to change the User-Agent of the handshake
        ws.set_option(websocket::stream_base::decorator(
            [](websocket::request_type& req)
            {
                req.set(http::field::user_agent,
                    std::string("agent"));
            }));

        ws.set_option(websocket::stream_base::decorator(set_subprotocols{"action = subscribe"}));
        ws.set_option(websocket::stream_base::decorator(set_subprotocols{"userID = madhur@ayraa.io"}));
        ws.set_option(websocket::stream_base::decorator(set_subprotocols{"agentID = 831C5DFC-1643-40C4-A5A3-9C918556D3A1"}));

        // Perform the websocket handshake
        ws.handshake(host, "/");
        // Send the message
        ws.write(net::buffer(std::string(text)));
        // This buffer will hold the incoming message
        beast::multi_buffer buffer;
        
        // Read a message into our buffer
        ws.read(buffer);
        // The make_printable() function helps print a ConstBufferSequence
        std::cout << beast::make_printable(buffer.data()) << std::endl;
        // If we get here then the connection is closed gracefully
        // Close the WebSocket connection
        ws.close(websocket::close_code::normal);
        
    }
    catch(std::exception const& e)
    {
        std::cerr << "Error: " << e.what() << std::endl;
    }
    return EXIT_SUCCESS;
}

我目前没有服务器代码,但如果我将来得到它,我会提供它。

【问题讨论】:

    标签: c++ boost websocket boost-asio


    【解决方案1】:

    您应该在一个装饰器中完成所有这些工作。事实上,你可以使用 lambda:

    ws.set_option(websocket::stream_base::decorator([](auto& m) {
        m.set("action", "subscribe");
        m.set("userID", "madhur@ayraa.io");
        m.set("agentID", "831C5DFC-1643-40C4-A5A3-9C918556D3A1");
    }));
    

    现在您的要求是:

    GET / HTTP/1.1
    Host: 127.0.0.1
    Upgrade: websocket
    Connection: upgrade
    Sec-WebSocket-Key: xCj08Lz6IzEzH4s422aT5w==
    Sec-WebSocket-Version: 13
    action: subscribe
    userID: madhur@ayraa.io
    agentID: 831C5DFC-1643-40C4-A5A3-9C918556D3A1
    
    HTTP/1.1 101 Switching Protocols
    Upgrade: websocket
    Connection: Upgrade
    Sec-WebSocket-Accept: pbg8RLbrPirBbYgmG0EptdCm2tQ=
    
    ...8.x.].......T..r{
     "timestamp": "Wed Dec 29 2021 20:20:27 GMT+0000 (UTC)",
     "url": "http://sockb.in/",
     "reqData": "hello world"
    }.. .H.
    B....
    

    【讨论】:

    • 如何为远程 WebSocket 调整此代码,因为它不能在远程 WebSocket 上工作,我需要在远程 WebSocket 服务器上对其进行测试。
    猜你喜欢
    • 1970-01-01
    • 2022-06-24
    • 1970-01-01
    • 2020-06-29
    • 2013-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-01
    相关资源
    最近更新 更多