C++ 如何实现websocket++;ping处理程序?
我试图通过在websocket++应用程序上发送ping来检测在不发送关闭帧的情况下关闭的连接丢失 我在设置处理程序时遇到问题 我最初尝试将其设置为处理程序的设置方式: 这就产生了这个错误: 注:候选人为: websocketpp/endpoint.hpp:240:10:注意:void websocketpp::endpoint::set_ping_处理程序(websocketpp::ping_处理程序)[带有connection=websocketpp::connection;config=websocketpp::config::asio_tls_客户端;websocketpp::ping_处理程序=std::function,std::basic_string)>] 无效集\u ping\u处理程序(ping\u处理程序h){ 我以为用设置一个C++ 如何实现websocket++;ping处理程序?,c++,websocket,handler,ping,websocket++,C++,Websocket,Handler,Ping,Websocket++,我试图通过在websocket++应用程序上发送ping来检测在不发送关闭帧的情况下关闭的连接丢失 我在设置处理程序时遇到问题 我最初尝试将其设置为处理程序的设置方式: 这就产生了这个错误: 注:候选人为: websocketpp/endpoint.hpp:240:10:注意:void websocketpp::endpoint::set_ping_处理程序(websocketpp::ping_处理程序)[带有connection=websocketpp::connection;config=w
typedef
就可以解决这个问题,但是把它放在类广播服务器
之外就不可能访问m\u服务器
如何正确实现这个处理程序
包括标志(&F)
增加1.54
#include <websocketpp/config/asio.hpp>
#include <websocketpp/server.hpp>
#include <websocketpp/common/thread.hpp>
typedef websocketpp::server<websocketpp::config::asio_tls> server;
typedef
typedef-websocketpp::lib::函数ping\u处理程序;
解决非常简单。首先,websocket/connection.hpp中的定义:
/// The type and function signature of a ping handler
/**
* The ping handler is called when the connection receives a WebSocket ping
* control frame. The string argument contains the ping payload. The payload is
* a binary string up to 126 bytes in length. The ping handler returns a bool,
* true if a pong response should be sent, false if the pong response should be
* suppressed.
*/
typedef lib::function<bool(connection_hdl,std::string)> ping_handler;
现在一切都到了正确的地方:
m_server.set_ping_handler(bind(&broadcast_server::on_ping,this,::_1,::_2));
完整的修改示例源如下所示:
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>
#include <iostream>
/*#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/condition_variable.hpp>*/
#include <websocketpp/common/thread.hpp>
typedef websocketpp::server<websocketpp::config::asio> server;
using websocketpp::connection_hdl;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
using websocketpp::lib::thread;
using websocketpp::lib::mutex;
using websocketpp::lib::unique_lock;
using websocketpp::lib::condition_variable;
/* on_open insert connection_hdl into channel
* on_close remove connection_hdl from channel
* on_message queue send to all channels
*/
enum action_type {
SUBSCRIBE,
UNSUBSCRIBE,
MESSAGE
};
struct action {
action(action_type t, connection_hdl h) : type(t), hdl(h) {}
action(action_type t, connection_hdl h, server::message_ptr m)
: type(t), hdl(h), msg(m) {}
action_type type;
websocketpp::connection_hdl hdl;
server::message_ptr msg;
};
class broadcast_server {
public:
broadcast_server() {
// Initialize Asio Transport
m_server.init_asio();
// Register handler callbacks
m_server.set_open_handler(bind(&broadcast_server::on_open,this,::_1));
m_server.set_close_handler(bind(&broadcast_server::on_close,this,::_1));
m_server.set_message_handler(bind(&broadcast_server::on_message,this,::_1,::_2));
m_server.set_ping_handler(bind(&broadcast_server::on_ping,this,::_1,::_2));
}
void run(uint16_t port) {
// listen on specified port
m_server.listen(port);
// Start the server accept loop
m_server.start_accept();
// Start the ASIO io_service run loop
try {
m_server.run();
} catch (const std::exception & e) {
std::cout << e.what() << std::endl;
} catch (websocketpp::lib::error_code e) {
std::cout << e.message() << std::endl;
} catch (...) {
std::cout << "other exception" << std::endl;
}
}
void on_open(connection_hdl hdl) {
unique_lock<mutex> lock(m_action_lock);
//std::cout << "on_open" << std::endl;
m_actions.push(action(SUBSCRIBE,hdl));
lock.unlock();
m_action_cond.notify_one();
}
void on_close(connection_hdl hdl) {
unique_lock<mutex> lock(m_action_lock);
//std::cout << "on_close" << std::endl;
m_actions.push(action(UNSUBSCRIBE,hdl));
lock.unlock();
m_action_cond.notify_one();
}
void on_message(connection_hdl hdl, server::message_ptr msg) {
// queue message up for sending by processing thread
unique_lock<mutex> lock(m_action_lock);
//std::cout << "on_message" << std::endl;
m_actions.push(action(MESSAGE,hdl,msg));
lock.unlock();
m_action_cond.notify_one();
}
bool on_ping(connection_hdl hdl, std::string s)
{
/* Do something */
return true;
}
void process_messages() {
while(1) {
unique_lock<mutex> lock(m_action_lock);
while(m_actions.empty()) {
m_action_cond.wait(lock);
}
action a = m_actions.front();
m_actions.pop();
lock.unlock();
if (a.type == SUBSCRIBE) {
unique_lock<mutex> con_lock(m_connection_lock);
m_connections.insert(a.hdl);
} else if (a.type == UNSUBSCRIBE) {
unique_lock<mutex> con_lock(m_connection_lock);
m_connections.erase(a.hdl);
} else if (a.type == MESSAGE) {
unique_lock<mutex> con_lock(m_connection_lock);
con_list::iterator it;
for (it = m_connections.begin(); it != m_connections.end(); ++it) {
m_server.send(*it,a.msg);
}
} else {
// undefined.
}
}
}
private:
typedef std::set<connection_hdl,std::owner_less<connection_hdl>> con_list;
server m_server;
con_list m_connections;
std::queue<action> m_actions;
mutex m_action_lock;
mutex m_connection_lock;
condition_variable m_action_cond;
};
int main() {
try {
broadcast_server server_instance;
// Start a thread to run the processing loop
thread t(bind(&broadcast_server::process_messages,&server_instance));
// Run the asio loop with the main thread
server_instance.run(9002);
t.join();
} catch (std::exception & e) {
std::cout << e.what() << std::endl;
}
}
#包括
#包括
#包括
/*#包括
#包括
#包括*/
#包括
typedef websocketpp::server;
使用websocketpp::connection_hdl;
使用websocketpp::lib::占位符::\u 1;
使用websocketpp::lib::占位符::\u 2;
使用websocketpp::lib::bind;
使用websocketpp::lib::thread;
使用websocketpp::lib::mutex;
使用websocketpp::lib::unique_lock;
使用websocketpp::lib::condition_变量;
/*打开时将连接插入hdl到通道中
*打开\u关闭从通道中删除连接\u hdl
*on_消息队列发送到所有通道
*/
枚举操作类型{
订阅
退订,
消息
};
结构动作{
动作(动作类型t,连接类型h):类型(t),hdl(h){}
操作(操作类型t,连接类型h,服务器::消息类型m)
:类型(t)、hdl(h)、msg(m){}
动作类型;
websocketpp::连接\u hdl hdl;
服务器::消息\u ptr msg;
};
类广播服务器{
公众:
广播服务器(){
//初始化Asio传输
m_server.init_asio();
//寄存器处理程序回调
m_server.set_open_处理程序(绑定(&broadcast_server::on_open,this,:_1));
m_server.set_close_处理程序(绑定(&broadcast_server::on_close,this,::_1));
m_server.set_message_handler(绑定(&broadcast_server::on_message,this,:::::::_1,:::_2));
m_-server.set_-ping_处理程序(绑定(&broadcast_-server::on_-ping,this,:::::_1,:::_2));
}
无效运行(uint16\u t端口){
//在指定端口上侦听
m_server.listen(端口);
//启动服务器接受循环
m_server.start_accept();
//启动ASIO io_服务运行循环
试一试{
m_server.run();
}捕获(const std::exception&e){
std::你能使用独立的asio和C++11还是boost::asio?boost::bind和std::bind的工作原理是不同的。如果你能精确描述所用软件包的版本以及boost与std的对比情况,我想我可以回答这个问题。@Tanuki谢谢你Tanuki!我希望已经编辑了你要求的详细信息。提前非常感谢你!可以吗你把标志STD=C++ +0x替换为-STD= C++ 11,看看会发生什么?我希望你的GCC高于4.7.3(第一个支持C++ 2011标准的好的)@ Tanuki谢谢Tuuki!尝试了每一个组合的代码< BIDE< /C++ >,仍然没有。作为一个旁注,这些标志不是相同的吗?再次感谢你们的帮助。刚刚从Git和WebSoCKPP中更新了。给我一些时间。
/// The type and function signature of a ping handler
/**
* The ping handler is called when the connection receives a WebSocket ping
* control frame. The string argument contains the ping payload. The payload is
* a binary string up to 126 bytes in length. The ping handler returns a bool,
* true if a pong response should be sent, false if the pong response should be
* suppressed.
*/
typedef lib::function<bool(connection_hdl,std::string)> ping_handler;
bool on_ping(connection_hdl hdl, std::string s)
{
/* Do something */
return true;
}
m_server.set_ping_handler(bind(&broadcast_server::on_ping,this,::_1,::_2));
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>
#include <iostream>
/*#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/condition_variable.hpp>*/
#include <websocketpp/common/thread.hpp>
typedef websocketpp::server<websocketpp::config::asio> server;
using websocketpp::connection_hdl;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
using websocketpp::lib::thread;
using websocketpp::lib::mutex;
using websocketpp::lib::unique_lock;
using websocketpp::lib::condition_variable;
/* on_open insert connection_hdl into channel
* on_close remove connection_hdl from channel
* on_message queue send to all channels
*/
enum action_type {
SUBSCRIBE,
UNSUBSCRIBE,
MESSAGE
};
struct action {
action(action_type t, connection_hdl h) : type(t), hdl(h) {}
action(action_type t, connection_hdl h, server::message_ptr m)
: type(t), hdl(h), msg(m) {}
action_type type;
websocketpp::connection_hdl hdl;
server::message_ptr msg;
};
class broadcast_server {
public:
broadcast_server() {
// Initialize Asio Transport
m_server.init_asio();
// Register handler callbacks
m_server.set_open_handler(bind(&broadcast_server::on_open,this,::_1));
m_server.set_close_handler(bind(&broadcast_server::on_close,this,::_1));
m_server.set_message_handler(bind(&broadcast_server::on_message,this,::_1,::_2));
m_server.set_ping_handler(bind(&broadcast_server::on_ping,this,::_1,::_2));
}
void run(uint16_t port) {
// listen on specified port
m_server.listen(port);
// Start the server accept loop
m_server.start_accept();
// Start the ASIO io_service run loop
try {
m_server.run();
} catch (const std::exception & e) {
std::cout << e.what() << std::endl;
} catch (websocketpp::lib::error_code e) {
std::cout << e.message() << std::endl;
} catch (...) {
std::cout << "other exception" << std::endl;
}
}
void on_open(connection_hdl hdl) {
unique_lock<mutex> lock(m_action_lock);
//std::cout << "on_open" << std::endl;
m_actions.push(action(SUBSCRIBE,hdl));
lock.unlock();
m_action_cond.notify_one();
}
void on_close(connection_hdl hdl) {
unique_lock<mutex> lock(m_action_lock);
//std::cout << "on_close" << std::endl;
m_actions.push(action(UNSUBSCRIBE,hdl));
lock.unlock();
m_action_cond.notify_one();
}
void on_message(connection_hdl hdl, server::message_ptr msg) {
// queue message up for sending by processing thread
unique_lock<mutex> lock(m_action_lock);
//std::cout << "on_message" << std::endl;
m_actions.push(action(MESSAGE,hdl,msg));
lock.unlock();
m_action_cond.notify_one();
}
bool on_ping(connection_hdl hdl, std::string s)
{
/* Do something */
return true;
}
void process_messages() {
while(1) {
unique_lock<mutex> lock(m_action_lock);
while(m_actions.empty()) {
m_action_cond.wait(lock);
}
action a = m_actions.front();
m_actions.pop();
lock.unlock();
if (a.type == SUBSCRIBE) {
unique_lock<mutex> con_lock(m_connection_lock);
m_connections.insert(a.hdl);
} else if (a.type == UNSUBSCRIBE) {
unique_lock<mutex> con_lock(m_connection_lock);
m_connections.erase(a.hdl);
} else if (a.type == MESSAGE) {
unique_lock<mutex> con_lock(m_connection_lock);
con_list::iterator it;
for (it = m_connections.begin(); it != m_connections.end(); ++it) {
m_server.send(*it,a.msg);
}
} else {
// undefined.
}
}
}
private:
typedef std::set<connection_hdl,std::owner_less<connection_hdl>> con_list;
server m_server;
con_list m_connections;
std::queue<action> m_actions;
mutex m_action_lock;
mutex m_connection_lock;
condition_variable m_action_cond;
};
int main() {
try {
broadcast_server server_instance;
// Start a thread to run the processing loop
thread t(bind(&broadcast_server::process_messages,&server_instance));
// Run the asio loop with the main thread
server_instance.run(9002);
t.join();
} catch (std::exception & e) {
std::cout << e.what() << std::endl;
}
}