gpt4 book ai didi

c++ - 如何实现 websocket++ ping 处理程序?

转载 作者:行者123 更新时间:2023-11-30 04:02:15 26 4
gpt4 key购买 nike

我正在尝试通过在 websocket++ 应用程序上发送 ping 来检测在未发送关闭帧的情况下关闭的丢失连接。

我在设置处理程序时遇到问题。

我最初尝试将其设置为使用 broadcast_server example 设置处理程序的方式:

m_server.set_ping_handler(bind(&broadcast_server::on_m_server_ping,this,::_1,::_2));

这给出了这个错误:

note: candidate is:

websocketpp/endpoint.hpp:240:10: note: void websocketpp::endpoint::set_ping_handler(websocketpp::ping_handler) [with connection = websocketpp::connection; config = websocketpp::config::asio_tls_client; websocketpp::ping_handler = std::function, std::basic_string)>]

void set_ping_handler(ping_handler h) {

我认为设置一个 typedef 就像 this problem会解决它,但将它放在 class broadcast_server 之外会导致无法访问 m_server

如何正确实现此处理程序?

包含和标记

提升 1.54

#include <websocketpp/config/asio.hpp>
#include <websocketpp/server.hpp>
#include <websocketpp/common/thread.hpp>
typedef websocketpp::server<websocketpp::config::asio_tls> server;

旗帜

-std=c++0x -I ~/broadcast_server -D_WEBSOCKETPP_CPP11_STL_ 
-D_WEBSOCKETPP_NO_CPP11_REGEX_ -lboost_regex -lboost_system
-lssl -lcrypto -pthread -lboost_thread

类型定义

typedef websocketpp::lib::function<bool(connection_hdl,std::string)> ping_handler;

最佳答案

解决起来很简单。一、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;

给出函数必须有定义的基本思想:

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;
}
}

关于c++ - 如何实现 websocket++ ping 处理程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25221566/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com