首页 > 解决方案 > 如何在 boost::beast 上保持 websocket?

问题描述

在这里引用了有关 boost::beast websocket-async 的文档。

我在 on_read 函数上删除了 "ws_.async_close(websocket::close_code::normal..." 以避免断开连接。

但是野兽 websocket 通过结束 on_read 断开连接。

std::make_shared<session>(ios)->run(host, port, text);
ios.run(); // I want to keep running it until explicit calling close.

标签: c++boostbeast

解决方案


替换代码

    // Close the WebSocket connection
    ws_.async_close(websocket::close_code::normal,
        std::bind(
            &session::on_close,
            shared_from_this(),
            std::placeholders::_1));

用你想要的逻辑。你想要发生什么?你想阅读更多吗?然后像上面一样再读一遍:

    // Read a message into our buffer

推荐阅读