LIB-ZC, 一个跨平台(Linux)平台通用C/C++扩展库,redis 客户端,订阅
本客户端基本用法见 https://blog.youkuaiyun.com/eli960/article/details/147220768
如何订阅redis服务器的频道
第一步, 创建客户端
#include "zcc/zcc_redis.h"
zcc::redis_client rc;
if (!rc.connect(zcc::var_main_config.get_cstring("server", "127.0.0.1:6379")))
{
std::printf("open error\n");
zcc::exit(1);
}
第二步, 订阅频道
rc.subscribe("news.abc");
rc.subscribe("news.xxx");
rc.unsubscribe("news.gogogo");
第三步,获取消息
for (; ;)
{
// rc.subscribe("news.xxx");
std::string type, channel, data;
// 10秒 超时
int ret = rc.fetch_channel_message(type, channel, data, 10);
if (ret < 0)
{
std::printf("No.%03d found error\n", i);
break;
}
if (ret == 0)
{
std::printf("No.%03d no message, timeout\n", i);
}
else
{
std::printf("No.%03d type: %s, channel: %s, data: %s\n", i, data.c_str(), channel.c_str(), data.c_str());
}
}
完整例子
https://gitee.com/linuxmail/lib-zc/blob/master/cpp_sample/redis/subscribe.cpp