Skip to content

Commit c005a87

Browse files
committed
add tls branch
1 parent 443a59a commit c005a87

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

src/internal/sio_client_impl.cpp

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ namespace sio
4949
m_client.set_close_handler(lib::bind(&client_impl::on_close,this,_1));
5050
m_client.set_fail_handler(lib::bind(&client_impl::on_fail,this,_1));
5151
m_client.set_message_handler(lib::bind(&client_impl::on_message,this,_1,_2));
52-
52+
#if SIO_TLS
53+
m_client.set_tls_init_handler(lib::bind(&client_impl::on_tls_init,this,_1));
54+
#endif
5355
m_packet_mgr.set_decode_callback(lib::bind(&client_impl::on_decode,this,_1));
5456

5557
m_packet_mgr.set_encode_callback(lib::bind(&client_impl::on_encode,this,_1,_2));
@@ -198,13 +200,17 @@ namespace sio
198200
do{
199201
websocketpp::uri uo(uri);
200202
ostringstream ss;
201-
203+
#if SIO_TLS
204+
ss<<"wss://";
205+
#else
206+
ss<<"ws://";
207+
#endif
202208
if (m_sid.size()==0) {
203-
ss<<"ws://"<<uo.get_host()<<":"<<uo.get_port()<<"/socket.io/?EIO=4&transport=websocket&t="<<time(NULL)<<queryString;
209+
ss<<uo.get_host()<<":"<<uo.get_port()<<"/socket.io/?EIO=4&transport=websocket&t="<<time(NULL)<<queryString;
204210
}
205211
else
206212
{
207-
ss<<"ws://"<<uo.get_host()<<":"<<uo.get_port()<<"/socket.io/?EIO=4&transport=websocket&sid="<<m_sid<<"&t="<<time(NULL)<<queryString;
213+
ss<<uo.get_host()<<":"<<uo.get_port()<<"/socket.io/?EIO=4&transport=websocket&sid="<<m_sid<<"&t="<<time(NULL)<<queryString;
208214
}
209215
lib::error_code ec;
210216
client_type::connection_ptr con = m_client.get_connection(ss.str(), ec);
@@ -549,4 +555,21 @@ namespace sio
549555
m_sid.clear();
550556
m_packet_mgr.reset();
551557
}
558+
559+
#if SIO_TLS
560+
client_impl::context_ptr client_impl::on_tls_init(connection_hdl conn)
561+
{
562+
context_ptr ctx = context_ptr(new boost::asio::ssl::context(boost::asio::ssl::context::tlsv1));
563+
boost::system::error_code ec;
564+
ctx->set_options(boost::asio::ssl::context::default_workarounds |
565+
boost::asio::ssl::context::no_sslv2 |
566+
boost::asio::ssl::context::single_dh_use,ec);
567+
if(ec)
568+
{
569+
cerr<<"Init tls failed,reason:"<< ec.message()<<endl;
570+
}
571+
572+
return ctx;
573+
}
574+
#endif
552575
}

src/internal/sio_client_impl.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,22 @@
1414
#endif
1515
#include <websocketpp/client.hpp>
1616
#if _DEBUG || DEBUG
17+
#if SIO_TLS
18+
#include <websocketpp/config/debug_asio.hpp>
19+
typedef websocketpp::config::debug_asio_tls client_config;
20+
#else
1721
#include <websocketpp/config/debug_asio_no_tls.hpp>
1822
typedef websocketpp::config::debug_asio client_config;
23+
#endif //SIO_TLS
24+
#else
25+
#if SIO_TLS
26+
#include <websocketpp/config/asio_client.hpp>
27+
typedef websocketpp::config::asio_tls_client client_config;
1928
#else
2029
#include <websocketpp/config/asio_no_tls_client.hpp>
2130
typedef websocketpp::config::asio_client client_config;
22-
#endif
31+
#endif //SIO_TLS
32+
#endif //DEBUG
2333
#include <boost/asio/deadline_timer.hpp>
2434

2535
#include <memory>
@@ -159,6 +169,12 @@ namespace sio
159169

160170
void clear_timers();
161171

172+
#if SIO_TLS
173+
typedef websocketpp::lib::shared_ptr<boost::asio::ssl::context> context_ptr;
174+
175+
context_ptr on_tls_init(connection_hdl con);
176+
#endif
177+
162178
// Connection pointer for client functions.
163179
connection_hdl m_con;
164180
client_type m_client;

0 commit comments

Comments
 (0)