如何将boost beast multi_buffer转换为字符串?

时间:2017-08-18 17:18:54

标签: c++ boost boost-beast

我从boost :: beast网站复制websocket示例并运行它的Websocket会话工作正常,但我不知道如何将收到的multi_buffer转换为字符串。

下面的代码是websocket会话处理程序。

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

有没有办法将缓冲区转换为字符串?

2 个答案:

答案 0 :(得分:12)

您可以在 <splash src="splash.png" /> <icon src="icon.png"/> <preference name="SplashScreen" value="screen" /> <preference name="SplashScreenDelay" value="10000" /> <platform name="android"> <preference name="SplashScreen" value="res/screens/android/drawable-land-ldpi-screen.png" /> <splash src="res/screens/android/drawable-land-ldpi-screen.png"/> <icon density="ldpi" src="res/icons/android/drawable-ldpi-icon.png" /> <icon density="mdpi" src="res/icons/android/drawable-mdpi-icon.png" /> <icon density="hdpi" src="res/icons/android/drawable-hdpi-icon.png" /> <icon density="xhdpi" src="res/icons/android/drawable-xhdpi-icon.png" /> <icon density="xxhdpi" src="res/icons/android/drawable-xxhdpi-icon.png" /> <icon density="xxxhdpi" src="res/icons/android/drawable-xxxhdpi-icon.png" /> <splash density="land-ldpi" src="res/screens/android/drawable-land-ldpi-screen.png" /> <splash density="land-mdpi" src="res/screens/android/drawable-land-mdpi-screen.png" /> <splash density="land-hdpi" src="res/screens/android/drawable-land-hdpi-screen.png" /> <splash density="land-xhdpi" src="res/screens/android/drawable-land-xhdpi-screen.png" /> <splash density="land-xxhdpi" src="res/screens/android/drawable-land-xxhdpi-screen.png" /> <splash density="land-xxxhdpi" src="res/screens/android/drawable-land-xxxhdpi-screen.png" /> <splash density="port-ldpi" src="res/screens/android/drawable-port-ldpi-screen.png" /> <splash density="port-mdpi" src="res/screens/android/drawable-port-mdpi-screen.png" /> <splash density="port-hdpi" src="res/screens/android/drawable-port-hdpi-screen.png" /> <splash density="port-xhdpi" src="res/screens/android/drawable-port-xhdpi-screen.png" /> <splash density="port-xxhdpi" src="res/screens/android/drawable-port-xxhdpi-screen.png" /> <splash density="port-xxxhdpi" src="res/screens/android/drawable-port-xxxhdpi-screen.png" /> </platform> <platform name="ios"> <preference name="SplashScreen" value="res/screens/ios/Default-568h@2x~iphone.png" /> <splash src="res/screens/ios/Default-568h@2x~iphone.png"/> <icon height="57" platform="ios" src="res/icons/ios/icon.png" width="57" /> <icon height="114" platform="ios" src="res/icons/ios/icon@2x.png" width="114" /> <icon height="40" platform="ios" src="res/icons/ios/icon-40.png" width="40" /> <icon height="80" platform="ios" src="res/icons/ios/icon-40@2x.png" width="80" /> <icon height="50" platform="ios" src="res/icons/ios/icon-50.png" width="50" /> <icon height="100" platform="ios" src="res/icons/ios/icon-50@2x.png" width="100" /> <icon height="60" platform="ios" src="res/icons/ios/icon-60.png" width="60" /> <icon height="120" platform="ios" src="res/icons/ios/icon-60@2x.png" width="120" /> <icon height="180" platform="ios" src="res/icons/ios/icon-60@3x.png" width="180" /> <icon height="72" platform="ios" src="res/icons/ios/icon-72.png" width="72" /> <icon height="144" platform="ios" src="res/icons/ios/icon-72@2x.png" width="144" /> <icon height="76" platform="ios" src="res/icons/ios/icon-76.png" width="76" /> <icon height="152" platform="ios" src="res/icons/ios/icon-76@2x.png" width="152" /> <icon height="29" platform="ios" src="res/icons/ios/icon-small.png" width="29" /> <icon height="58" platform="ios" src="res/icons/ios/icon-small@2x.png" width="58" /> <icon height="87" platform="ios" src="res/icons/ios/icon-small@3x.png" width="87" /> <splash height="1136" platform="ios" src="res/screens/ios/Default-568h@2x~iphone.png" width="640" /> <splash height="1334" platform="ios" src="res/screens/ios/Default-667h.png" width="750" /> <splash height="2208" platform="ios" src="res/screens/ios/Default-736h.png" width="1242" /> <splash height="1242" platform="ios" src="res/screens/ios/Default-Landscape-736h.png" width="2208" /> <splash height="1536" platform="ios" src="res/screens/ios/Default-Landscape@2x~ipad.png" width="2048" /> <splash height="768" platform="ios" src="res/screens/ios/Default-Landscape~ipad.png" width="1024" /> <splash height="2048" platform="ios" src="res/screens/ios/Default-Portrait@2x~ipad.png" width="1536" /> <splash height="1024" platform="ios" src="res/screens/ios/Default-Portrait~ipad.png" width="768" /> <splash height="960" platform="ios" src="res/screens/ios/Default@2x~iphone.png" width="640" /> <splash height="480" platform="ios" src="res/screens/ios/Default~iphone.png" width="320" /> </platform>

上使用buffers
buffer.data()

答案 1 :(得分:1)

由于最初的问题是关于不使用流而直接转换为字符串的问题,所以我决定添加我的答案。

您可以使用beast::buffers_to_string(buffer.data())

相关问题