boost::beast::websocket 服务器端获取客户端请求的URL相对路径(URL RVA PATH)

 WebSocket,URL格式:

WS协议://{主机}:{端口}{RVA PATH},比如:ws://121.40.165.18:8800

协议:WS(HTTP)、WSS(HTTPS),本文则提示 boost::beast::websocket 如何获取 {RVA PATH} 的内容,否则按照 boost::beast::websocket 提供的默认实现,会 accept 任何 RVA_PATH的WS连接请求,按照正常的WS服务器设计是不被允许的。

可用于调试 boost::beast::websocket 服务器端的在线测试网页。

websocket在线测试 (websocket-test.com)

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#include 
#include 
#include 
#include 
#include 

using tcp = boost::asio::ip::tcp;
namespace websocket = boost::beast::websocket;

// echoes back all received WebSocket messages
void do_session(tcp::socket& socket)
{
    try {
        // Construct the stream by moving in the socket
        websocket::stream ws{ std::move(socket) };

        // This buffer is used for reading and must be persisted
        boost::beast::flat_buffer buffer;

        // Declare a container to hold the response
        boost::beast::http::request req;

        // Receive the HTTP response
        boost::system::error_code ec;
        boost::beast::http::read(ws.next_layer(), buffer, req, ec);
        if (ec == boost::beast::http::error::end_of_stream)
            ec = boost::beast::websocket::error::closed;
        if (ec)
            return;

        // Accept the websocket handshake
        ws.accept(req);

        // Print path.
        printf("path: %s\n", std::string(req.target().data(), req.target().length()).data());
        for (;;) {
            // This buffer will hold the incoming message
            boost::beast::multi_buffer buffer;

            // Read a message
            ws.read(buffer);

            // Echo the message back
            ws.text(ws.got_text());
            ws.write(buffer.data());
        }
    }
    catch (boost::system::system_error const& se) {
        // This indicates that the session was closed
        if (se.code() != websocket::error::closed)
            std::cerr << "Error: " << se.code().message() << std::endl;
    }
    catch (std::exception const& e) {
        std::cerr << "Error: " << e.what() << std::endl;
    }
}

int main(int argc, char* argv[])
{
    try {
        // Check command line arguments
        if (argc != 3) {
            std::cerr <<
                "Usage: server 
\n" << "Example:\n" << " server 0.0.0.0 8080\n"; return EXIT_FAILURE; } auto const address = boost::asio::ip::make_address(argv[1]); auto const port = static_cast(std::atoi(argv[2])); // The io_context is required for all I/O boost::asio::io_context ioc{ 1 }; // The acceptor receives incoming connections tcp::acceptor acceptor{ ioc, {address, port} }; for (;;) { // This will receive the new connection tcp::socket socket{ ioc }; // Block until we get a connection acceptor.accept(socket); // Launch the session, transferring ownership of the socket std::thread{ std::bind(&do_session, std::move(socket)) }.detach(); } } catch (const std::exception& e) { std::cerr << "Error: " << e.what() << std::endl; return EXIT_FAILURE; } }

你可能感兴趣的:(C/C++,websocket,网络协议,网络)