mirror of
https://github.com/socketio/socket.io-client-cpp.git
synced 2026-06-09 19:54:46 +00:00
Merge remote-tracking branch 'origin/tls'
This commit is contained in:
commit
c617682373
@ -1,54 +1,6 @@
|
||||
cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR)
|
||||
PROJECT(sioclient)
|
||||
|
||||
function(get_lib_name out_lib_name component)
|
||||
if(MSVC)
|
||||
set(${out_lib_name} "${component}.lib" PARENT_SCOPE)
|
||||
else()
|
||||
set(${out_lib_name} "lib${component}.a" PARENT_SCOPE)
|
||||
endif()
|
||||
endfunction()
|
||||
|
||||
function(install_debug_libs lib_dir)
|
||||
get_lib_name(renamed_system "boost_system")
|
||||
install(FILES ${Boost_SYSTEM_LIBRARY_DEBUG}
|
||||
CONFIGURATIONS "Debug" DESTINATION ${lib_dir} RENAME ${renamed_system}
|
||||
)
|
||||
get_lib_name(renamed_date_time "boost_date_time")
|
||||
install(FILES ${Boost_DATE_TIME_LIBRARY_DEBUG}
|
||||
CONFIGURATIONS "Debug" DESTINATION ${lib_dir} RENAME ${renamed_date_time}
|
||||
)
|
||||
get_lib_name(renamed_random "boost_random")
|
||||
install(FILES ${Boost_RANDOM_LIBRARY_DEBUG}
|
||||
CONFIGURATIONS "Debug" DESTINATION ${lib_dir} RENAME ${renamed_random}
|
||||
)
|
||||
|
||||
install(TARGETS sioclient
|
||||
CONFIGURATIONS "Debug" DESTINATION ${lib_dir}
|
||||
)
|
||||
endfunction()
|
||||
|
||||
function(install_release_libs lib_dir)
|
||||
get_lib_name(renamed_system "boost_system")
|
||||
install(FILES ${Boost_SYSTEM_LIBRARY_RELEASE}
|
||||
CONFIGURATIONS "Release" DESTINATION ${lib_dir} RENAME ${renamed_system}
|
||||
)
|
||||
get_lib_name(renamed_date_time "boost_date_time")
|
||||
install(FILES ${Boost_DATE_TIME_LIBRARY_RELEASE}
|
||||
CONFIGURATIONS "Release" DESTINATION ${lib_dir} RENAME ${renamed_date_time}
|
||||
)
|
||||
get_lib_name(renamed_random "boost_random")
|
||||
install(FILES ${Boost_RANDOM_LIBRARY_RELEASE}
|
||||
CONFIGURATIONS "Release" DESTINATION ${lib_dir} RENAME ${renamed_random}
|
||||
)
|
||||
|
||||
install(TARGETS sioclient
|
||||
CONFIGURATIONS "Release" DESTINATION ${lib_dir}
|
||||
)
|
||||
endfunction()
|
||||
|
||||
|
||||
MESSAGE(STATUS ${CMAKE_CURRENT_LIST_DIR})
|
||||
if(NOT CMAKE_BUILD_TYPE )
|
||||
MESSAGE(STATUS "not define build type, set to release" )
|
||||
set(CMAKE_BUILD_TYPE Release )
|
||||
@ -67,7 +19,7 @@ find_package(Boost ${BOOST_VER} REQUIRED COMPONENTS system date_time random)
|
||||
aux_source_directory(${CMAKE_CURRENT_LIST_DIR}/src ALL_SRC)
|
||||
aux_source_directory(${CMAKE_CURRENT_LIST_DIR}/src/internal ALL_SRC)
|
||||
file(GLOB ALL_HEADERS ${CMAKE_CURRENT_LIST_DIR}/src/*.h )
|
||||
MESSAGE(STATUS ${ALL_HEADERS} )
|
||||
set(SIO_INCLUDEDIR ${CMAKE_CURRENT_LIST_DIR})
|
||||
|
||||
add_library(sioclient STATIC ${ALL_SRC})
|
||||
target_include_directories(sioclient PRIVATE ${Boost_INCLUDE_DIRS}
|
||||
@ -79,12 +31,32 @@ target_include_directories(sioclient PRIVATE ${Boost_INCLUDE_DIRS}
|
||||
set_property(TARGET sioclient PROPERTY CXX_STANDARD 11)
|
||||
set_property(TARGET sioclient PROPERTY CXX_STANDARD_REQUIRED ON)
|
||||
target_link_libraries(sioclient PRIVATE ${Boost_LIBRARIES})
|
||||
|
||||
find_package(OpenSSL)
|
||||
if(OPENSSL_FOUND)
|
||||
add_library(sioclient_tls STATIC ${ALL_SRC})
|
||||
target_include_directories(sioclient_tls PRIVATE ${Boost_INCLUDE_DIRS}
|
||||
${CMAKE_CURRENT_LIST_DIR}/src
|
||||
${CMAKE_CURRENT_LIST_DIR}/lib/websocketpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/lib/rapidjson/include
|
||||
${OPENSSL_INCLUDE_DIR}
|
||||
)
|
||||
|
||||
set_property(TARGET sioclient_tls PROPERTY CXX_STANDARD 11)
|
||||
set_property(TARGET sioclient_tls PROPERTY CXX_STANDARD_REQUIRED ON)
|
||||
target_link_libraries(sioclient_tls PRIVATE ${Boost_LIBRARIES} ${OPENSSL_LIBRARIES} )
|
||||
target_compile_definitions(sioclient_tls PRIVATE -DSIO_TLS)
|
||||
|
||||
endif()
|
||||
|
||||
install(FILES ${ALL_HEADERS}
|
||||
DESTINATION "${CMAKE_CURRENT_LIST_DIR}/build/include"
|
||||
)
|
||||
)
|
||||
|
||||
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||
install_debug_libs("${CMAKE_CURRENT_LIST_DIR}/build/lib/${CMAKE_BUILD_TYPE}" )
|
||||
else()
|
||||
install_release_libs("${CMAKE_CURRENT_LIST_DIR}/build/lib/${CMAKE_BUILD_TYPE}" )
|
||||
endif()
|
||||
install(TARGETS sioclient
|
||||
DESTINATION "${CMAKE_CURRENT_LIST_DIR}/build/lib/${CMAKE_BUILD_TYPE}"
|
||||
)
|
||||
|
||||
install(FILES ${Boost_LIBRARIES}
|
||||
DESTINATION "${CMAKE_CURRENT_LIST_DIR}/build/lib/${CMAKE_BUILD_TYPE}"
|
||||
)
|
||||
|
||||
10
examples/Console/CMakeLists.txt
Normal file
10
examples/Console/CMakeLists.txt
Normal file
@ -0,0 +1,10 @@
|
||||
cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR)
|
||||
include(${CMAKE_CURRENT_SOURCE_DIR}/../../CMakeLists.txt)
|
||||
add_executable(sio_console_demo main.cpp)
|
||||
set_property(TARGET sio_console_demo PROPERTY CXX_STANDARD 11)
|
||||
set_property(TARGET sio_console_demo PROPERTY CXX_STANDARD_REQUIRED ON)
|
||||
target_link_libraries(sio_console_demo sioclient)
|
||||
target_link_libraries(sio_console_demo pthread )
|
||||
message(STATUS ${Boost_INCLUDE_DIRS} )
|
||||
#target_include_directories(sio_console_demo PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../src" ${Boost_INCLUDE_DIRS} )
|
||||
|
||||
@ -71,16 +71,16 @@ socket::ptr current_socket;
|
||||
|
||||
void bind_events(socket::ptr &socket)
|
||||
{
|
||||
current_socket->on("new message",(sio::socket::event_listener_aux) [&](string const& name, message::ptr const& data, bool isAck,message::ptr &ack_resp)
|
||||
current_socket->on("new message", sio::socket::event_listener_aux([&](string const& name, message::ptr const& data, bool isAck,message::ptr &ack_resp)
|
||||
{
|
||||
_lock.lock();
|
||||
string user = data->get_map()["username"]->get_string();
|
||||
string message = data->get_map()["message"]->get_string();
|
||||
EM(user<<":"<<message);
|
||||
_lock.unlock();
|
||||
});
|
||||
}));
|
||||
|
||||
current_socket->on("user joined", (sio::socket::event_listener_aux)[&](string const& name, message::ptr const& data, bool isAck,message::ptr &ack_resp)
|
||||
current_socket->on("user joined",sio::socket::event_listener_aux([&](string const& name, message::ptr const& data, bool isAck,message::ptr &ack_resp)
|
||||
{
|
||||
_lock.lock();
|
||||
string user = data->get_map()["username"]->get_string();
|
||||
@ -90,8 +90,8 @@ void bind_events(socket::ptr &socket)
|
||||
// abc "
|
||||
HIGHLIGHT(user<<" joined"<<"\nthere"<<(plural?" are ":"'s ")<< participants<<(plural?" participants":" participant"));
|
||||
_lock.unlock();
|
||||
});
|
||||
current_socket->on("user left",(sio::socket::event_listener_aux) [&](string const& name, message::ptr const& data, bool isAck,message::ptr &ack_resp)
|
||||
}));
|
||||
current_socket->on("user left", sio::socket::event_listener_aux([&](string const& name, message::ptr const& data, bool isAck,message::ptr &ack_resp)
|
||||
{
|
||||
_lock.lock();
|
||||
string user = data->get_map()["username"]->get_string();
|
||||
@ -99,7 +99,7 @@ void bind_events(socket::ptr &socket)
|
||||
bool plural = participants !=1;
|
||||
HIGHLIGHT(user<<" left"<<"\nthere"<<(plural?" are ":"'s ")<< participants<<(plural?" participants":" participant"));
|
||||
_lock.unlock();
|
||||
});
|
||||
}));
|
||||
}
|
||||
|
||||
MAIN_FUNC
|
||||
@ -126,7 +126,7 @@ Login:
|
||||
|
||||
getline(cin, nickname);
|
||||
}
|
||||
current_socket->on("login", (sio::socket::event_listener_aux)[&](string const& name, message::ptr const& data, bool isAck,message::ptr &ack_resp){
|
||||
current_socket->on("login", sio::socket::event_listener_aux([&](string const& name, message::ptr const& data, bool isAck,message::ptr &ack_resp){
|
||||
_lock.lock();
|
||||
participants = data->get_map()["numUsers"]->get_int();
|
||||
bool plural = participants !=1;
|
||||
@ -134,7 +134,7 @@ Login:
|
||||
_cond.notify_all();
|
||||
_lock.unlock();
|
||||
current_socket->off("login");
|
||||
});
|
||||
}));
|
||||
current_socket->emit("add user", nickname);
|
||||
_lock.lock();
|
||||
if (participants<0) {
|
||||
|
||||
@ -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));
|
||||
@ -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);
|
||||
@ -549,4 +555,21 @@ failed:
|
||||
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
|
||||
}
|
||||
|
||||
@ -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>
|
||||
@ -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;
|
||||
|
||||
@ -151,11 +151,21 @@ namespace sio
|
||||
:message(flag_string),_v(v)
|
||||
{
|
||||
}
|
||||
|
||||
string_message(string&& v)
|
||||
:message(flag_string),_v(move(v))
|
||||
{
|
||||
}
|
||||
public:
|
||||
static message::ptr create(string const& v)
|
||||
{
|
||||
return ptr(new string_message(v));
|
||||
}
|
||||
|
||||
static message::ptr create(string&& v)
|
||||
{
|
||||
return ptr(new string_message(move(v)));
|
||||
}
|
||||
|
||||
string const& get_string() const
|
||||
{
|
||||
@ -258,15 +268,20 @@ namespace sio
|
||||
m_vector.push_back(message);
|
||||
}
|
||||
|
||||
list(string& text)
|
||||
list(const string& text)
|
||||
{
|
||||
m_vector.push_back(string_message::create(text));
|
||||
}
|
||||
|
||||
list(const char* text)
|
||||
list(string&& text)
|
||||
{
|
||||
if(text)
|
||||
m_vector.push_back(string_message::create(text));
|
||||
m_vector.push_back(string_message::create(move(text)));
|
||||
}
|
||||
|
||||
list(shared_ptr<string> const& binary)
|
||||
{
|
||||
if(binary)
|
||||
m_vector.push_back(binary_message::create(binary));
|
||||
}
|
||||
|
||||
list(shared_ptr<const string> const& binary)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user