确实提升::asio支持websocket

Does boost::asio support websockets?

本文关键字:asio websocket 支持      更新时间:2023-10-16

我之前发布了一个问题,问我的服务器(用C++和boost::asio编写)为什么不能与客户端(用Javascript编写)连接。问题是Javascript Websockets与boost::asio套接字不同吗?boost::asio不支持websocket吗?解决这个问题最简单的方法是什么?

Boost.Bast,现在是Boost的一部分,构建在Boost.Asio之上,按照您期望的方式工作。它附带了示例代码和文档。点击此处查看:www.boost.org/libs/beast

下面是一个完整的程序,它向echo服务器发送一条消息:

#include <boost/beast/core.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/asio/connect.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <cstdlib>
#include <iostream>
#include <string>
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
int main(int argc, char** argv)
{
    try
    {
        // Check command line arguments.
        if(argc != 4)
        {
            std::cerr <<
                "Usage: websocket-client-sync <host> <port> <text>n" <<
                "Example:n" <<
                "    websocket-client-sync echo.websocket.org 80 "Hello, world!"n";
            return EXIT_FAILURE;
        }
        std::string host = argv[1];
        auto const  port = argv[2];
        auto const  text = argv[3];
        // 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);
        // Make the connection on the IP address we get from a lookup
        auto ep = net::connect(ws.next_layer(), results);
        // Update the host_ string. This will provide the value of the
        // Host HTTP header during the WebSocket handshake.
        // See https://tools.ietf.org/html/rfc7230#section-5.4
        host += ':' + std::to_string(ep.port());
        // 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(BOOST_BEAST_VERSION_STRING) +
                        " websocket-client-coro");
            }));
        // 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::flat_buffer buffer;
        // Read a message into our buffer
        ws.read(buffer);
        // Close the WebSocket connection
        ws.close(websocket::close_code::normal);
        // If we get here then the connection is closed gracefully
        // The make_printable() function helps print a ConstBufferSequence
        std::cout << beast::make_printable(buffer.data()) << std::endl;
    }
    catch(std::exception const& e)
    {
        std::cerr << "Error: " << e.what() << std::endl;
        return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
}