@@ -204,36 +204,36 @@ std::pair<std::string, std::string> WsConnection::make_request(std::string url,
204204// /////////////////////// Class methods /////////////////////////////////
205205// ////////////////////////////////////////////////////////////////////////
206206
207- WsConnection::WsConnection ()
207+ WsConnection::WsConnection (bool enable_tls )
208208 : m_status(ConnectionStatus::CLOSED),
209209 m_on_open(NULL ),
210210 m_on_message(NULL ),
211211 m_reconnect_attempts(0 ) {
212- // set the endpoint logging behavior to silent by clearing all of the access and error
213- // logging channels
214- m_endpoint.clear_access_channels (websocketpp::log::alevel::all);
215- m_endpoint.clear_error_channels (websocketpp::log::elevel::all);
216-
217- // try {
218- m_endpoint.init_asio ();
219- // } catch (const websocketpp::exception& e) {
220- // std::cout << "CAUGHT ERROR!!!" << e.m_msg << std::endl;
221- // }
222- #ifdef BASED_TLS
223- m_endpoint.set_tls_init_handler (websocketpp::lib::bind (&WsConnection::on_tls_init));
224- #endif
225-
226- // perpetual mode = the endpoint's processing loop will not exit automatically when it has
227- // no connections
228- m_endpoint.start_perpetual ();
229- // run perpetually in a thread
230-
231- m_thread = std::make_shared<std::thread>(&ws_client::run, &m_endpoint);
212+ m_enable_tls = enable_tls;
213+ BASED_LOG (" ENABLE TLS = %d" , m_enable_tls);
214+ if (m_enable_tls) {
215+ m_wss_endpoint.clear_access_channels (websocketpp::log::alevel::all);
216+ m_wss_endpoint.clear_error_channels (websocketpp::log::elevel::all);
217+ m_wss_endpoint.init_asio ();
218+ m_wss_endpoint.set_tls_init_handler (websocketpp::lib::bind (&WsConnection::on_tls_init));
219+ m_wss_endpoint.start_perpetual ();
220+ m_thread = std::make_shared<std::thread>(&wss_client::run, &m_wss_endpoint);
221+ } else {
222+ m_ws_endpoint.clear_access_channels (websocketpp::log::alevel::all);
223+ m_ws_endpoint.clear_error_channels (websocketpp::log::elevel::all);
224+ m_ws_endpoint.init_asio ();
225+ m_ws_endpoint.start_perpetual ();
226+ m_thread = std::make_shared<std::thread>(&ws_client::run, &m_ws_endpoint);
227+ }
232228};
233229
234230WsConnection::~WsConnection () {
235231 disconnect ();
236- m_endpoint.stop_perpetual ();
232+ if (m_enable_tls) {
233+ m_wss_endpoint.stop_perpetual ();
234+ } else {
235+ m_ws_endpoint.stop_perpetual ();
236+ }
237237 m_thread->join ();
238238
239239 BASED_LOG (" Destroyed WsConnection obj" );
@@ -256,11 +256,11 @@ std::string WsConnection::discover_service(BasedConnectOpt opts, bool http) {
256256 auto service_url = req.first ;
257257 auto access_key = req.second ;
258258 url = service_url;
259- # ifdef BASED_TLS
260- return http ? " https://" + url : " wss://" + url + " /" + access_key;
261- # else
262- return http ? " http://" + url : " ws://" + url + " /" + access_key;
263- # endif
259+ if (m_enable_tls) {
260+ return http ? " https://" + url : " wss://" + url + " /" + access_key;
261+ } else {
262+ return http ? " http://" + url : " ws://" + url + " /" + access_key;
263+ }
264264}
265265
266266void WsConnection::connect (std::string cluster,
@@ -303,27 +303,51 @@ void WsConnection::connect_to_uri(std::string uri) {
303303
304304 m_uri = uri;
305305 websocketpp::lib::error_code ec;
306- ws_client::connection_ptr con = m_endpoint.get_connection (m_uri, ec);
307306
308- if (ec) {
309- BASED_LOG (" Error trying to initialize connection, message = \" %s\" " , ec.message ().c_str ());
310- m_status = ConnectionStatus::FAILED;
311- return ;
312- }
307+ if (m_enable_tls) {
308+ wss_client::connection_ptr con = m_wss_endpoint.get_connection (m_uri, ec);
309+
310+ if (ec) {
311+ BASED_LOG (" Error trying to initialize connection, message = \" %s\" " ,
312+ ec.message ().c_str ());
313+ m_status = ConnectionStatus::FAILED;
314+ return ;
315+ }
316+
317+ m_status = ConnectionStatus::CONNECTING;
313318
314- m_status = ConnectionStatus::CONNECTING;
319+ // Add special headers
315320
316- // Add special headers
321+ con-> append_header ( " sec-websocket-protocol " , " {} " );
317322
318- con->append_header ( " sec-websocket-protocol " , " {} " );
323+ m_hdl = con->get_handle ( );
319324
320- m_hdl = con-> get_handle ( );
325+ set_handlers (con );
321326
322- set_handlers (con);
327+ m_wss_endpoint. connect (con);
323328
324- m_endpoint.connect (con);
329+ } else {
330+ ws_client::connection_ptr con = m_ws_endpoint.get_connection (m_uri, ec);
325331
326- return ;
332+ if (ec) {
333+ BASED_LOG (" Error trying to initialize connection, message = \" %s\" " ,
334+ ec.message ().c_str ());
335+ m_status = ConnectionStatus::FAILED;
336+ return ;
337+ }
338+
339+ m_status = ConnectionStatus::CONNECTING;
340+
341+ // Add special headers
342+
343+ con->append_header (" sec-websocket-protocol" , " {}" );
344+
345+ m_hdl = con->get_handle ();
346+
347+ set_handlers (con);
348+
349+ m_ws_endpoint.connect (con);
350+ }
327351};
328352
329353void WsConnection::set_open_handler (std::function<void ()> on_open) {
@@ -343,7 +367,11 @@ void WsConnection::disconnect() {
343367 m_status = ConnectionStatus::TERMINATED_BY_USER;
344368
345369 websocketpp::lib::error_code ec;
346- m_endpoint.close (m_hdl, websocketpp::close::status::going_away, " " , ec);
370+ if (m_enable_tls) {
371+ m_wss_endpoint.close (m_hdl, websocketpp::close::status::going_away, " " , ec);
372+ } else {
373+ m_ws_endpoint.close (m_hdl, websocketpp::close::status::going_away, " " , ec);
374+ }
347375 if (ec) {
348376 BASED_LOG (" Error trying to close connection, message = \" %s\" " , ec.message ().c_str ());
349377
@@ -356,7 +384,13 @@ void WsConnection::send(std::vector<uint8_t> message) {
356384
357385 if (m_status != ConnectionStatus::OPEN) throw (std::runtime_error (" Connection is not open." ));
358386
359- m_endpoint.send (m_hdl, message.data (), message.size (), websocketpp::frame::opcode::binary, ec);
387+ if (m_enable_tls) {
388+ m_wss_endpoint.send (m_hdl, message.data (), message.size (),
389+ websocketpp::frame::opcode::binary, ec);
390+ } else {
391+ m_ws_endpoint.send (m_hdl, message.data (), message.size (),
392+ websocketpp::frame::opcode::binary, ec);
393+ }
360394 if (ec) {
361395 BASED_LOG (" Error trying to send message, message = \" %s\" " , ec.message ().c_str ());
362396 return ;
@@ -367,10 +401,7 @@ ConnectionStatus WsConnection::status() {
367401 return m_status;
368402};
369403
370- #ifdef BASED_TLS
371- #ifdef ASIO_STANDALONE
372404using context_ptr = std::shared_ptr<asio::ssl::context>;
373-
374405context_ptr WsConnection::on_tls_init () {
375406 context_ptr ctx = std::make_shared<asio::ssl::context>(asio::ssl::context::sslv23);
376407
@@ -383,27 +414,59 @@ context_ptr WsConnection::on_tls_init() {
383414 }
384415 return ctx;
385416}
386- #else
387- using context_ptr = std::shared_ptr<boost::asio::ssl::context>;
388417
389- context_ptr WsConnection::on_tls_init () {
390- context_ptr ctx =
391- std::make_shared<boost::asio::ssl::context>(boost::asio::ssl::context::sslv23);
418+ void WsConnection::set_handlers (ws_client::connection_ptr con) {
419+ // bind must be used if the function we're binding to doest have the right number of
420+ // arguments (hence the placeholders) these handlers must be set before calling connect, and
421+ // can't be changed after (i think)
422+ con->set_open_handler ([this ](websocketpp::connection_hdl) {
423+ BASED_LOG (" Connection opened" );
424+ m_status = ConnectionStatus::OPEN;
425+ m_reconnect_attempts = 0 ;
426+ if (m_on_open) {
427+ m_on_open ();
428+ }
429+ });
392430
393- try {
394- ctx->set_options (boost::asio::ssl::context::default_workarounds |
395- boost::asio::ssl::context::no_sslv2 | boost::asio::ssl::context::no_sslv3 |
396- boost::asio::ssl::context::single_dh_use);
431+ con->set_message_handler ([this ](websocketpp::connection_hdl hdl, ws_client::message_ptr msg) {
432+ // here we will pass the message to the decoder, which, based on the header, will
433+ // call the appropriate callback
397434
398- } catch (std::exception& e) {
399- std::cerr << " Error in context pointer: " << e.what () << std::endl;
400- }
401- return ctx;
435+ std::string payload = msg->get_payload ();
436+ if (m_on_message) {
437+ m_on_message (payload);
438+ }
439+ });
440+
441+ con->set_close_handler ([this ](websocketpp::connection_hdl) {
442+ if (m_status != ConnectionStatus::TERMINATED_BY_USER) {
443+ m_status = ConnectionStatus::CLOSED;
444+ m_reconnect_attempts++;
445+
446+ if (!m_opts.name .empty ()) {
447+ connect (m_opts.cluster , m_opts.org , m_opts.project , m_opts.env , m_opts.key ,
448+ m_opts.optional_key , m_opts.host , m_opts.discovery_url );
449+ } else {
450+ connect_to_uri (m_uri);
451+ }
452+ }
453+ });
454+
455+ con->set_fail_handler ([this ](websocketpp::connection_hdl) {
456+ BASED_LOG (" Received FAIL event" );
457+ m_status = ConnectionStatus::FAILED;
458+ m_reconnect_attempts++;
459+
460+ if (m_uri.size () == 0 ) {
461+ connect (m_opts.cluster , m_opts.org , m_opts.project , m_opts.env , m_opts.key ,
462+ m_opts.optional_key , m_opts.host , m_opts.discovery_url );
463+ } else {
464+ connect_to_uri (m_uri);
465+ }
466+ });
402467}
403- #endif
404- #endif
405468
406- void WsConnection::set_handlers (ws_client ::connection_ptr con) {
469+ void WsConnection::set_handlers (wss_client ::connection_ptr con) {
407470 // bind must be used if the function we're binding to doest have the right number of
408471 // arguments (hence the placeholders) these handlers must be set before calling connect, and
409472 // can't be changed after (i think)
@@ -416,7 +479,7 @@ void WsConnection::set_handlers(ws_client::connection_ptr con) {
416479 }
417480 });
418481
419- con->set_message_handler ([this ](websocketpp::connection_hdl hdl, ws_client ::message_ptr msg) {
482+ con->set_message_handler ([this ](websocketpp::connection_hdl hdl, wss_client ::message_ptr msg) {
420483 // here we will pass the message to the decoder, which, based on the header, will
421484 // call the appropriate callback
422485
0 commit comments