Changing handlers after a connection is established

The following example demonstrates the use of set_*_handler to change a handler callback on a per connection basis after the connection has been established. This example starts by printing a message whenever any message comes in. If the message content is "upgrade" then the handler gets switched to the second handler. All future messages for that connection will use the new callback. Any handler may be changed any number of times.

#include <iostream>

#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.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::ref;

void custom_on_msg(server & s, connection_hdl hdl, server::message_ptr msg) {
        std::cout << "Message sent to custom handler" << std::endl;
}

void default_on_msg(server & s, connection_hdl hdl, server::message_ptr msg) {
    std::cout << "Message sent to default handler" << std::endl;
    
    if (msg->get_payload() == "upgrade") {
        // Upgrade our connection_hdl to a full connection_ptr
        server::connection_ptr con = s.get_con_from_hdl(hdl);
        
        // Change the on message handler for this connection only to 
        // custom_on_mesage
        con->set_message_handler(bind(&custom_on_msg,ref(s),::_1,::_2));
        std::cout << "Upgrading connection to custom handler" << std::endl;
    }
}

int main() {
    server s;

    s.set_message_handler(bind(&default_on_msg,ref(s),::_1,::_2));

    s.init_asio();
    s.listen(9002);
    s.start_accept();

    s.run();
}

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.