javascript - boost::asio 是否支持 websockets?

标签 javascript c++ sockets websocket boost-asio

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

最佳答案

Boost.Beast,现在是 Boost 的一部分,建立在 Boost.Asio 之上,并以您期望的方式工作。它带有示例代码和文档。在这里查看:www.boost.org/libs/beast

这是一个向回显服务器发送消息的完整程序:

#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;
}

关于javascript - boost::asio 是否支持 websockets?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36561179/

相关文章:

C++ 客户端套接字仅接收消息的第一个字母

c - rt linux中的用户程序如何访问或接收来自内核的数据?

javascript - 如何在 QML 中访问 ListView 的 ListModel 的 ListElement 的映射委托(delegate)数据?

javascript - 当属性位于另一个对象中时如何将 _.groupBy js 用于组对象 - TypeScript

c++ - (C++) 命令行剪刀石头布游戏的 If - Else if 语句

c++ - 我需要购买 Qt 框架吗?

c++ - 循环 rand(),在下一个 rand() 上不要使用前一个?

c - 带轮询的非阻塞套接字

javascript - Fine uploader 的自定义选择文件按钮部分无法识别点击

javascript - CodeMirror:有没有办法通过单击 Esc 按钮来存在搜索模式