Skip to content

Commit

Permalink
add tls branch
Browse files Browse the repository at this point in the history
  • Loading branch information
melode11 committed May 19, 2015
1 parent 443a59a commit c005a87
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
31 changes: 27 additions & 4 deletions src/internal/sio_client_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ namespace sio
m_client.set_close_handler(lib::bind(&client_impl::on_close,this,_1));
m_client.set_fail_handler(lib::bind(&client_impl::on_fail,this,_1));
m_client.set_message_handler(lib::bind(&client_impl::on_message,this,_1,_2));

#if SIO_TLS
m_client.set_tls_init_handler(lib::bind(&client_impl::on_tls_init,this,_1));
#endif
m_packet_mgr.set_decode_callback(lib::bind(&client_impl::on_decode,this,_1));

m_packet_mgr.set_encode_callback(lib::bind(&client_impl::on_encode,this,_1,_2));
Expand Down Expand Up @@ -198,13 +200,17 @@ namespace sio
do{
websocketpp::uri uo(uri);
ostringstream ss;

#if SIO_TLS
ss<<"wss://";
#else
ss<<"ws://";
#endif
if (m_sid.size()==0) {
ss<<"ws://"<<uo.get_host()<<":"<<uo.get_port()<<"/socket.io/?EIO=4&transport=websocket&t="<<time(NULL)<<queryString;
ss<<uo.get_host()<<":"<<uo.get_port()<<"/socket.io/?EIO=4&transport=websocket&t="<<time(NULL)<<queryString;
}
else
{
ss<<"ws://"<<uo.get_host()<<":"<<uo.get_port()<<"/socket.io/?EIO=4&transport=websocket&sid="<<m_sid<<"&t="<<time(NULL)<<queryString;
ss<<uo.get_host()<<":"<<uo.get_port()<<"/socket.io/?EIO=4&transport=websocket&sid="<<m_sid<<"&t="<<time(NULL)<<queryString;
}
lib::error_code ec;
client_type::connection_ptr con = m_client.get_connection(ss.str(), ec);
Expand Down Expand Up @@ -549,4 +555,21 @@ namespace sio
m_sid.clear();
m_packet_mgr.reset();
}

#if SIO_TLS
client_impl::context_ptr client_impl::on_tls_init(connection_hdl conn)
{
context_ptr ctx = context_ptr(new boost::asio::ssl::context(boost::asio::ssl::context::tlsv1));
boost::system::error_code ec;
ctx->set_options(boost::asio::ssl::context::default_workarounds |
boost::asio::ssl::context::no_sslv2 |
boost::asio::ssl::context::single_dh_use,ec);
if(ec)
{
cerr<<"Init tls failed,reason:"<< ec.message()<<endl;
}

return ctx;
}
#endif
}
18 changes: 17 additions & 1 deletion src/internal/sio_client_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,22 @@
#endif
#include <websocketpp/client.hpp>
#if _DEBUG || DEBUG
#if SIO_TLS
#include <websocketpp/config/debug_asio.hpp>
typedef websocketpp::config::debug_asio_tls client_config;
#else
#include <websocketpp/config/debug_asio_no_tls.hpp>
typedef websocketpp::config::debug_asio client_config;
#endif //SIO_TLS
#else
#if SIO_TLS
#include <websocketpp/config/asio_client.hpp>
typedef websocketpp::config::asio_tls_client client_config;
#else
#include <websocketpp/config/asio_no_tls_client.hpp>
typedef websocketpp::config::asio_client client_config;
#endif
#endif //SIO_TLS
#endif //DEBUG
#include <boost/asio/deadline_timer.hpp>

#include <memory>
Expand Down Expand Up @@ -159,6 +169,12 @@ namespace sio

void clear_timers();

#if SIO_TLS
typedef websocketpp::lib::shared_ptr<boost::asio::ssl::context> context_ptr;

context_ptr on_tls_init(connection_hdl con);
#endif

// Connection pointer for client functions.
connection_hdl m_con;
client_type m_client;
Expand Down

0 comments on commit c005a87

Please sign in to comment.