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

时间:2014-08-09 18:05:45

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

我正在尝试通过在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; config = websocketpp :: config :: asio_tls_client; websocketpp :: ping_handler = std :: function,std :: basic_string)>]

     

void set_ping_handler(ping_handler h){

我认为设置与this problem类似的typedef会解决此问题,但将其置于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;

1 个答案:

答案 0 :(得分:2)

解决很容易。首先,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;
    }
}