windows c++使用hiredis同步模式实现发布订阅
来源:互联网 发布:北京南站离北京站源码 编辑:程序博客网 时间:2024/06/06 03:13
//参考网址
http://blog.csdn.net/lls2012/article/details/71123099
#include <iostream>
using namespace std;#include"hiredis.h"
int main()
{
//初始化
int ret = redisNetInit();
if(ret == 0)
{
printf("redisNetInit fail!");
return -1;
}
//通过ip.port连接redis服务(默认监听端口为6379)
timeval t;
t.tv_sec = 500;
redisContext* redis = redisConnectWithTimeout("192.168.13.167", 6379,t);
if (NULL == redis || redis->err)
{
//释放redis连接
redisFree(redis);
printf("Connect to redisServer fail\n");
return -1;
}
printf("Connect to redisServer Success\n");
//REDIS://192.168.107.89.6379/2edcdb0c-4414-5631-b628-3f3c11bb2c7a
//SUBSCRIBE命令用于订阅给定的一个或多个频道的信息(SUBSCRIBE:发布订阅的命令)
std::string command = "SUBSCRIBE ";
command += "eventChannel";
redisReply * reply = (redisReply *)redisCommand(redis,command.c_str());
freeReplyObject(reply);
while (redisGetReply(redis, (void **)&reply) == REDIS_OK)
{
if (reply == NULL)
{
printf("Execut command failure\n");
// 命令执行失败,释放内存
Sleep(2000);
continue;
}
if (!(reply->type == REDIS_REPLY_ARRAY && reply->elements == 3))
{
// 判断命令执行的返回值
printf("Failed to execute command[%s]\n", command);
freeReplyObject(reply);
Sleep(2000);
continue;
}
//输出结果
for (int i = 0; i < reply->elements; i++)
{
printf("%d)%s\n", i + 1, reply->element[i]->str);
}
freeReplyObject(reply);
}
redisFree(redis);
while (1)
{
//执行redis数据库中的操作命令(结果强转成redisReply*类型)
redisReply* reply = (redisReply*)redisCommand(redis,command.c_str());
if (NULL == reply)
{
printf("Execut command failure\n");
// 命令执行失败,释放内存
Sleep(2000);
continue;
}
freeReplyObject(reply);
redisGetReply(redis, (void **)&reply);
if (!(reply->type == REDIS_REPLY_ARRAY && reply->elements == 3))
{
// 判断命令执行的返回值
printf("Failed to execute command[%s]\n", command);
freeReplyObject(reply);
Sleep(2000);
continue;
}
//输出结果
if(strcmp(reply->element[0]->str,"subscribe") != 0)
{
printf("%s",
reply->element[0]->st);//结果
printf("获取channel=》%s :%s",
reply->element[1]->str, //channel
reply->element[2]->str);//结果
printf("获取channel=》%s :%s",
reply->element[1]->str, //channel
reply->element[2]->str);//结果
}
//释放redisCommand执行后返回的redisReply所占用的内存
freeReplyObject(reply);
Sleep(2000);
}
#endif
return 0;
}
- windows c++使用hiredis同步模式实现发布订阅
- windows c++使用hiredis同步模式实现发布订阅
- hiredis发布/订阅示例
- hiredis发布/订阅示例
- linux下使用hiredis异步API实现sub/pub消息订阅和发布的功能
- linux下使用hiredis异步API实现sub/pub消息订阅和发布的功能
- linux下使用hiredis异步API实现sub/pub消息订阅和发布的功能 标签: hiredishiredis异步APIhiredis事件处理redis消息订阅发布redis c接口 2016-
- [SQL Server] 使用事务发布与订阅实现数据同步
- SQL Server 2005使用发布、订阅实现数据同步
- windows下hiredis使用
- windows下hiredis使用
- 发布订阅者模式c++实现
- 订阅发布模式——C++实现
- 用 JavaScript 实现发布/订阅模式
- 使用Autobahn的订阅/发布模式
- activeMQ发布订阅者模式使用
- MQTT协议之使用Future模式订阅及发布(使用fusesource mqtt-client实现)
- MQTT协议之使用Future模式订阅及发布(使用fusesource mqtt-client实现)
- JAVA中常用的Map和Collection数据结构图解
- SQUASHFS error 解决
- 博弈论--从 必胜点与必败点 到 SG 函数
- hdu3452 最小割
- ACM训练日记—8月17日
- windows c++使用hiredis同步模式实现发布订阅
- 操作系统笔试题
- zoj3204(最小生成树)
- 如何通过出色的设计让你的简历脱颖而出
- 关于NoClassDefFoundError和ClassNotFoundException异常
- 8.17--练习赛F题--Moo Volume
- 配置MySQL数据库的一般查询日志
- SQL注入学习总结
- HDU6140 Hybrid Crystals 签到题