add tls branch

This commit is contained in:
melode11 2015-05-19 14:45:56 +08:00
parent 443a59a7f0
commit c005a8792d
2 changed files with 44 additions and 5 deletions

View File

@ -49,7 +49,9 @@ namespace sio
m_client.set_close_handler(lib::bind(&client_impl::on_close,this,_1)); 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_fail_handler(lib::bind(&client_impl::on_fail,this,_1));
m_client.set_message_handler(lib::bind(&client_impl::on_message,this,_1,_2)); 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_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)); m_packet_mgr.set_encode_callback(lib::bind(&client_impl::on_encode,this,_1,_2));
@ -198,13 +200,17 @@ namespace sio
do{ do{
websocketpp::uri uo(uri); websocketpp::uri uo(uri);
ostringstream ss; ostringstream ss;
#if SIO_TLS
ss<<"wss://";
#else
ss<<"ws://";
#endif
if (m_sid.size()==0) { 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 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; lib::error_code ec;
client_type::connection_ptr con = m_client.get_connection(ss.str(), ec); client_type::connection_ptr con = m_client.get_connection(ss.str(), ec);
@ -549,4 +555,21 @@ failed:
m_sid.clear(); m_sid.clear();
m_packet_mgr.reset(); 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
} }

View File

@ -14,12 +14,22 @@
#endif #endif
#include <websocketpp/client.hpp> #include <websocketpp/client.hpp>
#if _DEBUG || DEBUG #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> #include <websocketpp/config/debug_asio_no_tls.hpp>
typedef websocketpp::config::debug_asio client_config; 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 #else
#include <websocketpp/config/asio_no_tls_client.hpp> #include <websocketpp/config/asio_no_tls_client.hpp>
typedef websocketpp::config::asio_client client_config; typedef websocketpp::config::asio_client client_config;
#endif #endif //SIO_TLS
#endif //DEBUG
#include <boost/asio/deadline_timer.hpp> #include <boost/asio/deadline_timer.hpp>
#include <memory> #include <memory>
@ -159,6 +169,12 @@ namespace sio
void clear_timers(); 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 pointer for client functions.
connection_hdl m_con; connection_hdl m_con;
client_type m_client; client_type m_client;