adds support for message_handler

This commit is contained in:
Peter Thorson
2013-01-08 06:33:02 -06:00
parent 8df37fffbc
commit ef8898afef
5 changed files with 36 additions and 2 deletions

View File

@@ -89,9 +89,20 @@ void on_tcp_init(websocketpp::connection_hdl hdl) {
std::cout << "on_tcp_init called with hdl: " << hdl.lock().get() << std::endl;
}
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
// pull out the type of messages sent by our config
typedef websocketpp::config::asio::message_type::ptr message_ptr;
void on_message(server* s,websocketpp::connection_hdl hdl,message_ptr msg) {
std::cout << "on_message called with hdl: " << hdl.lock().get()
<< " and message: " << msg->get_payload()
<< std::endl;
//s->get_con_from_hdl(hdl)->send(msg->get_payload(),msg->get_opcode());
}
int main() {
server::handler::ptr h(new handler());
@@ -102,10 +113,10 @@ int main() {
test_handler t(echo_server);
//echo_server.set_open_handler(websocketpp::lib::bind(&test_handler::on_open,t,websocketpp::lib::placeholders::_1));
echo_server.set_open_handler(bind(&on_open,&echo_server,::_1));
echo_server.set_interrupt_handler(bind(&on_interrupt,&echo_server,::_1));
echo_server.set_tcp_init_handler(&on_tcp_init);
echo_server.set_message_handler(bind(&on_message,&echo_server,::_1,::_2));
// Listen
echo_server.listen(9002);

View File

@@ -181,6 +181,9 @@ public:
typedef processor::processor<config> processor_type;
typedef lib::shared_ptr<processor_type> processor_ptr;
// Message handler (needs to know message type)
typedef lib::function<void(connection_hdl,message_ptr)> message_handler;
// Misc Convenience Types
typedef session::internal_state::value istate_type;
@@ -340,6 +343,16 @@ public:
m_validate_handler = h;
}
/// Set message handler
/**
* The message handler is called after a new message has been received.
*
* @param h The new message_handler
*/
void set_message_handler(message_handler h) {
m_message_handler = h;
}
/// Set new connection handler
/**
* Will invoke the old handler's on_unload callback followed by the
@@ -832,6 +845,7 @@ private:
interrupt_handler m_interrupt_handler;
http_handler m_http_handler;
validate_handler m_validate_handler;
message_handler m_message_handler;
/// Legacy Handler
handler_ptr m_handler;

View File

@@ -65,6 +65,9 @@ public:
/// that this endpoint creates.
typedef typename transport_con_type::ptr transport_con_ptr;
/// Type of message_handler
typedef typename connection_type::message_handler message_handler;
// TODO: organize these
typedef typename connection_type::handler handler_type;
typedef typename handler_type::ptr handler_ptr;
@@ -125,6 +128,7 @@ public:
void set_interrupt_handler(interrupt_handler h) {m_interrupt_handler = h;}
void set_http_handler(http_handler h) {m_http_handler = h;}
void set_validate_handler(validate_handler h) {m_validate_handler = h;}
void set_message_handler(message_handler h) {m_message_handler = h;}
/*************************************/
/* Connection pass through functions */
@@ -185,6 +189,7 @@ private:
interrupt_handler m_interrupt_handler;
http_handler m_http_handler;
validate_handler m_validate_handler;
message_handler m_message_handler;
// endpoint resources
std::set<connection_ptr> m_connections;

View File

@@ -685,7 +685,10 @@ void connection<config>::handle_read_frame(const lib::error_code& ec,
std::cout << "null message from m_processor" << std::endl;
} else if (!is_control(msg->get_opcode())) {
// data message, dispatch to user
m_handler->on_message(type::shared_from_this(), msg);
//m_handler->on_message(type::shared_from_this(), msg);
if (m_message_handler) {
m_message_handler(m_connection_hdl, msg);
}
} else {
process_control_frame(msg);
}

View File

@@ -67,6 +67,7 @@ endpoint<connection,config>::create_connection() {
con->set_interrupt_handler(m_interrupt_handler);
con->set_http_handler(m_http_handler);
con->set_validate_handler(m_validate_handler);
con->set_message_handler(m_message_handler);
con->set_termination_handler(
lib::bind(