Sarodh Uggalla
Sarodh Uggalla

Reputation: 303

How to establish a secure socket connection with the c++ library websocket++?

I can establish a regular WS connection using the utility client here, https://github.com/zaphoyd/websocketpp/blob/master/tutorials/utility_client/step6.cpp

However I need to attempt a secure WS connection and have replaced the config with the config below and linked the required libraries.

typedef websocketpp::client<websocketpp::config::asio_tls_client> client;

My handler looks like:

typedef std::shared_ptr<boost::asio::ssl::context> context_ptr;

// part of class connection_metadata
static context_ptr on_tls_init(websocketpp::connection_hdl) {
    context_ptr ctx = std::make_shared<boost::asio::ssl::context>(boost::asio::ssl::context::sslv23);

    try {
        ctx->set_options(boost::asio::ssl::context::default_workarounds |
                         boost::asio::ssl::context::no_sslv2 |
                         boost::asio::ssl::context::no_sslv3 |
                         boost::asio::ssl::context::single_dh_use);
    } catch (std::exception& e) {
        std::cout <<"Error in context pointer: "<< e.what() << std::endl;
    }
    return ctx;
}

// part of class websocket_endpoint 
con->set_tls_init_handler(bind(&connection_metadata::on_tls_init,metadata_ptr,std::placeholders::_1));

When I attempt the following line to get connection:

client::connection_ptr con = m_endpoint.get_connection(uri, ec);

I receive:

Connection creation attempt failed

Upvotes: 3

Views: 4397

Answers (1)

Sarodh Uggalla
Sarodh Uggalla

Reputation: 303

I have solved the issue thanks to: https://groups.google.com/forum/#!topic/websocketpp/SimAUzwZUVM

I had to make on_tls_init a static function and use it like so (before get connection) : m_endpoint.set_tls_init_handler(connection_metadata::on_tls_init)

where m_endpoint is a client object.

Upvotes: 1

Related Questions