【问题标题】:Retrieve the code/reason from a close message从关闭消息中检索代码/原因
【发布时间】:2015-03-12 16:00:25
【问题描述】:

我想从关闭消息中检索代码和原因。我已经用set_close_handler 注册了一个处理程序,但它没有得到有效载荷。另外,我发现websocketpp::close::extract_codewebsocketpp::close::extract_reason 接收有效载荷并返回相关部分。关闭的有效载荷是否存储在某个地方? connection_ptr 中是否有最后一个代码/原因?

【问题讨论】:

    标签: websocket++


    【解决方案1】:

    连接对象有两个用于此目的的访问器方法:

    close::status::value get_remote_close_code() const;
    std::string const & get_remote_close_reason() const;
    

    on_close 方法是通过connection_hdl 调用的,所以需要调用get_con_from_hdl() 来获取指向该连接的指针。这是一个例子:

    void chat_server::on_close(connection_hdl hdl) {
        try {
            server::connection_ptr cp = m_server.get_con_from_hdl(hdl);
            websocketpp::close::status::value ccode = cp->get_remote_close_code();
            std::cout << "Closed connection. code " << ccode << " ["
                << cp->get_remote_close_reason() << "]" << std::endl;
        } catch (const websocketpp::lib::error_code& e) {
            std::cout << __func__ << " failed because: " << e << "(" << e.message() << ")"
                 << std::endl;
        } catch (std::exception& e) {
            std::cout << e.what() << std::endl;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-18
      • 2020-03-16
      • 1970-01-01
      • 2019-04-30
      • 1970-01-01
      • 2022-01-25
      • 1970-01-01
      相关资源
      最近更新 更多