play with cpp_redis

This commit is contained in:
Benjamin Sergeant 2019-03-29 11:42:45 -07:00
parent 2e9c610ac9
commit 5e1a4541bf
4 changed files with 40 additions and 1 deletions

View File

@ -135,3 +135,4 @@ set( IXWEBSOCKET_INCLUDE_DIRS
target_include_directories( ixwebsocket PUBLIC ${IXWEBSOCKET_INCLUDE_DIRS} ) target_include_directories( ixwebsocket PUBLIC ${IXWEBSOCKET_INCLUDE_DIRS} )
add_subdirectory(ws) add_subdirectory(ws)
add_subdirectory(third_party/cpp_redis)

View File

@ -42,5 +42,5 @@ if (APPLE AND USE_TLS)
target_link_libraries(ws "-framework foundation" "-framework security") target_link_libraries(ws "-framework foundation" "-framework security")
endif() endif()
target_link_libraries(ws ixwebsocket) target_link_libraries(ws ixwebsocket cpp_redis tacopie)
install(TARGETS ws RUNTIME DESTINATION bin) install(TARGETS ws RUNTIME DESTINATION bin)

View File

@ -7,6 +7,7 @@
#include "IXRedisClient.h" #include "IXRedisClient.h"
#include <ixwebsocket/IXSocketFactory.h> #include <ixwebsocket/IXSocketFactory.h>
#include <ixwebsocket/IXSocket.h> #include <ixwebsocket/IXSocket.h>
#include <cpp_redis/cpp_redis>
#include <sstream> #include <sstream>
#include <iomanip> #include <iomanip>
@ -17,6 +18,14 @@ namespace ix
{ {
bool RedisClient::connect(const std::string& hostname, int port) 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; bool tls = false;
std::string errorMsg; std::string errorMsg;
_socket = createSocket(tls, errorMsg); _socket = createSocket(tls, errorMsg);
@ -28,11 +37,22 @@ namespace ix
std::string errMsg; std::string errMsg;
return _socket->connect(hostname, port, errMsg, nullptr); return _socket->connect(hostname, port, errMsg, nullptr);
} }
bool RedisClient::auth(const std::string& password, bool RedisClient::auth(const std::string& password,
std::string& response) 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(); response.clear();
if (!_socket) return false; if (!_socket) return false;
@ -60,6 +80,7 @@ namespace ix
response = line; response = line;
return lineValid; return lineValid;
#endif
} }
@ -101,6 +122,19 @@ namespace ix
const OnRedisSubscribeResponseCallback& responseCallback, const OnRedisSubscribeResponseCallback& responseCallback,
const OnRedisSubscribeCallback& callback) 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; if (!_socket) return false;
std::stringstream ss; std::stringstream ss;
@ -203,5 +237,6 @@ namespace ix
} }
return true; return true;
#endif
} }
} }

View File

@ -8,6 +8,7 @@
#include <memory> #include <memory>
#include <functional> #include <functional>
#include <cpp_redis/cpp_redis>
namespace ix namespace ix
{ {
@ -35,6 +36,8 @@ namespace ix
const OnRedisSubscribeCallback& callback); const OnRedisSubscribeCallback& callback);
private: private:
cpp_redis::subscriber _sub;
std::shared_ptr<Socket> _socket; std::shared_ptr<Socket> _socket;
}; };
} }