【问题标题】:How to implement websocket++ ping handler?如何实现 websocket++ ping 处理程序?
【发布时间】:2016-07-29 00:26:11
【问题描述】:

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

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

我最初尝试设置它,就像使用 broadcast_server example 设置处理程序一样:

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

这给出了这个错误:

注意:候选人是:

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

void set_ping_handler(ping_handler h) {

我认为设置typedefthis 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

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

【问题讨论】:

  • 你使用独立的 asio 和 C++11 还是 boost::asio? boost::bind 和 std::bind 的工作方式有所不同。如果您确定使用过的软件包的版本以及 boost vs std case - 我想我可以回答这个问题。
  • @Tanuki 谢谢Tanuki!我希望已根据您要求的详细信息进行了编辑。提前非常感谢您!
  • 你能不能把标志 -std=c++0x 换成 -std=c++11 看看会发生什么?希望你的GCC高于4.7.3(第一个支持C++ 2011标准的好版本)
  • @Tanuki 谢谢Tanuki!尝试了bind 的所有组合,仍然没有。作为旁注,这些标志不一样吗?再次非常感谢您的帮助!
  • 刚刚从 git 更新并构建 websocketpp 以进行提升。给我一些时间。

标签: c++ websocket handler ping websocket++


【解决方案1】:

解决起来很容易。一、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++ 家伙真的是天才!非常感谢!
猜你喜欢
  • 2016-04-10
  • 1970-01-01
  • 1970-01-01
  • 2022-10-07
  • 2015-02-10
  • 1970-01-01
  • 2021-08-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多