【问题标题】:Boost Beast reconnect websocket connection after close关闭后Boost Beast重新连接websocket连接
【发布时间】:2018-06-27 17:57:53
【问题描述】:

我正在使用 boost::beast 创建一个 websocket 连接,到目前为止它似乎工作正常。但是我无法确定是否应该在断开连接后替换 websocket::stream 变量。

即我有一个变量:

boost::beast::websocket::stream<boost::asio::ssl::stream<boost::asio::ip::tcp::socket>> _ws

启动并完成异步断开连接后:

bool WebsocketConnection::startDisconnectSequence()
{
    _ws.async_close(
        boost::beast::websocket::close_code::normal,
        boost::asio::bind_executor(
            _strand,
            std::bind(
                &WebsocketConnection::onClose,
                this,
                std::placeholders::_1)));

    return true;
}

如果我想重新启动,我应该替换 _ws 变量

    boost::asio::async_connect(
        _ws.next_layer().next_layer(),
        results.begin(),
        results.end(),
        boost::asio::bind_executor(
            _strand,
            std::bind(
                &ASIOConnectionBase::onConnect,
                this,
                std::placeholders::_1)));
}

【问题讨论】:

  • 你的发现是什么?

标签: c++ c++11 boost boost-asio boost-beast


【解决方案1】:

这应该可以工作,但老实说我还没有测试过。重复使用会发生什么?

【讨论】:

  • 作为 ssl 客户端,如果因网络中断而关闭,在重新连接期间,套接字看起来总是在 SSL 握手时出现“短读”错误。
  • 我不确定 Asio ssl 流是否可重用。我从来没有尝试过。
  • 我也在寻找同样问题的答案!
  • 我还希望实现一些“重新连接”逻辑,以及发送 PING 和 PONG。消息的服务器端可能会遇到 OnClose 事件,然后如果连接 WiFi 的客户端在大型仓库中滑入和滑出 WiFi 范围,则会触发“关闭”。所以看来我需要实现“重新连接”以避免仅仅因为 500 毫秒 WiFi 打嗝而强制应用程序将用户弹回登录屏幕。
猜你喜欢
  • 2019-01-13
  • 2012-11-27
  • 1970-01-01
  • 1970-01-01
  • 2021-04-27
  • 1970-01-01
  • 1970-01-01
  • 2019-10-22
  • 2012-08-08
相关资源
最近更新 更多