Feature/redis (#23)
* Fix warning * (cmake) add a warning about 32/64 conversion problems. * simple redis clients * can publish to redis * redis subscribe * display messages received per second * verbose flag * (cmake) use clang only compile option -Wshorten-64-to-32 when compiling with clang
This commit is contained in:
committed by
GitHub
parent
663299c91e
commit
0635313566
66
ws/ws_redis_subscribe.cpp
Normal file
66
ws/ws_redis_subscribe.cpp
Normal file
@ -0,0 +1,66 @@
|
||||
/*
|
||||
* ws_redis_subscribe.cpp
|
||||
* Author: Benjamin Sergeant
|
||||
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <chrono>
|
||||
#include "IXRedisClient.h"
|
||||
|
||||
namespace ix
|
||||
{
|
||||
int ws_redis_subscribe_main(const std::string& hostname,
|
||||
int port,
|
||||
const std::string& channel,
|
||||
bool verbose)
|
||||
{
|
||||
RedisClient redisClient;
|
||||
if (!redisClient.connect(hostname, port))
|
||||
{
|
||||
std::cerr << "Cannot connect to redis host" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::chrono::time_point<std::chrono::steady_clock> lastTimePoint;
|
||||
int msgPerSeconds = 0;
|
||||
int msgCount = 0;
|
||||
|
||||
auto callback = [&lastTimePoint, &msgPerSeconds, &msgCount, verbose]
|
||||
(const std::string& message)
|
||||
{
|
||||
if (verbose)
|
||||
{
|
||||
std::cout << message << std::endl;
|
||||
}
|
||||
|
||||
msgPerSeconds++;
|
||||
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
if (now - lastTimePoint > std::chrono::seconds(1))
|
||||
{
|
||||
lastTimePoint = std::chrono::steady_clock::now();
|
||||
|
||||
msgCount += msgPerSeconds;
|
||||
|
||||
// #messages 901 msg/s 150
|
||||
std::cout << "#messages " << msgCount << " "
|
||||
<< "msg/s " << msgPerSeconds
|
||||
<< std::endl;
|
||||
|
||||
msgPerSeconds = 0;
|
||||
}
|
||||
};
|
||||
|
||||
std::cerr << "Subscribing to " << channel << "..." << std::endl;
|
||||
if (!redisClient.subscribe(channel, callback))
|
||||
{
|
||||
std::cerr << "Error subscribing to channel " << channel << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user