我想从关闭消息中检索代码和原因。我已经用 注册了一个处理程序set_close_handler
,但它没有得到有效负载。另外,我发现了一个有效载荷websocketpp::close::extract_code
并websocketpp::close::extract_reason
返回相关部分。关闭的有效载荷是否存储在某个地方?中的最后一个代码/原因是否可用connection_ptr
?
问问题
188 次
1 回答
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;
}
}
于 2015-03-17T19:14:21.617 回答