【发布时间】: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