diff --git a/CMakeLists.txt b/CMakeLists.txt index 31f20a40..5af289df 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -135,3 +135,4 @@ set( IXWEBSOCKET_INCLUDE_DIRS target_include_directories( ixwebsocket PUBLIC ${IXWEBSOCKET_INCLUDE_DIRS} ) add_subdirectory(ws) +add_subdirectory(third_party/cpp_redis) diff --git a/ws/CMakeLists.txt b/ws/CMakeLists.txt index fe26f0b3..81ec91bf 100644 --- a/ws/CMakeLists.txt +++ b/ws/CMakeLists.txt @@ -42,5 +42,5 @@ if (APPLE AND USE_TLS) target_link_libraries(ws "-framework foundation" "-framework security") endif() -target_link_libraries(ws ixwebsocket) +target_link_libraries(ws ixwebsocket cpp_redis tacopie) install(TARGETS ws RUNTIME DESTINATION bin) diff --git a/ws/IXRedisClient.cpp b/ws/IXRedisClient.cpp index cf48a6f6..808b8509 100644 --- a/ws/IXRedisClient.cpp +++ b/ws/IXRedisClient.cpp @@ -7,6 +7,7 @@ #include "IXRedisClient.h" #include #include +#include #include #include @@ -17,6 +18,14 @@ namespace ix { bool RedisClient::connect(const std::string& hostname, int port) { + _sub.connect(hostname, port, [] + (const std::string& host, std::size_t port, cpp_redis::connect_state status) { + if (status == cpp_redis::connect_state::dropped) { + std::cout << "client disconnected from " << host << ":" << port << std::endl; + } + }); + + // also subscribe the old way bool tls = false; std::string errorMsg; _socket = createSocket(tls, errorMsg); @@ -28,11 +37,22 @@ namespace ix std::string errMsg; return _socket->connect(hostname, port, errMsg, nullptr); + } bool RedisClient::auth(const std::string& password, std::string& response) { + // authentication if server-server requires it + // _sub.auth(password, [&response](const cpp_redis::reply& reply) { + // if (reply.is_error()) { std::cerr << "Authentication failed: " << reply.as_string() << std::endl; } + // else { + // std::cout << "successful authentication" << std::endl; + // } + // }); + + return true; +#if 0 response.clear(); if (!_socket) return false; @@ -60,6 +80,7 @@ namespace ix response = line; return lineValid; +#endif } @@ -101,6 +122,19 @@ namespace ix const OnRedisSubscribeResponseCallback& responseCallback, const OnRedisSubscribeCallback& callback) { + _sub.subscribe(channel, [&callback](const std::string& chan, const std::string& msg) { + callback(msg); + }); + _sub.commit(); + + while (true) + { + auto duration = std::chrono::seconds(1); + std::this_thread::sleep_for(duration); + } + + return true; +#if 0 if (!_socket) return false; std::stringstream ss; @@ -203,5 +237,6 @@ namespace ix } return true; +#endif } } diff --git a/ws/IXRedisClient.h b/ws/IXRedisClient.h index ee53a7aa..8e64c0b1 100644 --- a/ws/IXRedisClient.h +++ b/ws/IXRedisClient.h @@ -8,6 +8,7 @@ #include #include +#include namespace ix { @@ -35,6 +36,8 @@ namespace ix const OnRedisSubscribeCallback& callback); private: + cpp_redis::subscriber _sub; + std::shared_ptr _socket; }; }