How to convert boost beast multi_buffer to string?

I copy the websocket example from boost :: beast and run it in a Websocket session, but I don’t know how to convert the resulting multi_buffer to a string.

the code below is the websocket session handler.

void
do_session(tcp::socket &socket) {
    try {
        // Construct the stream by moving in the socket
        websocket::stream <tcp::socket> ws{std::move(socket)};

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

        while (true) {
            // This buffer will hold the incoming message
            boost::beast::multi_buffer buffer;

            // Read a message
            boost::beast::error_code ec;
            ws.read(buffer, ec);

            if (ec == websocket::error::closed) {
                break;
            }

            // Echo the message back
            ws.text(ws.got_text());
            ws.write(buffer);
        }

        cout << "Close" << endl;
    }
    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;
    }
}

Is there a way to convert a buffer to a string?

+4
source share
1 answer

You can use buffers inbuffer.data()

std::cout << "Data read:   " << boost::beast::buffers(buffer.data()) << 
std::endl;
+5
source

Source: https://habr.com/ru/post/1683978/


All Articles