Redis实战之Win7 64 + VS2013 + Redis 3.0 订阅和发布

xiaoxiao2021-02-28  125

相关命令

向指定频道channel发送消息message: publish channel message 事例:publish RedisChat Hello world!

订阅频道: subscribe channel [channel …]

工程配置

1、C/C++:

General / Additional Include Directories: redis-3.0\deps、E:\redis-3.0\src

Preprocessor / Preprocessor Definitions: WIN32 _OFF_T_DEFINED LACKS_STDLIB_H NO_QFORKIMPL

Code Generation / Runtime Library: Debug:Multi-threaded Debug (/MTd) Release:Multi-threaded (/MT)

如果结合Qt使用,则可能出现如下错误: 需要在Preprocessor / Preprocessor Definitions中添加宏定义,但最终却不能使用STL: QT_DLL QT_NO_STL

2、Linker:

General / Additional Library Directories: Debug:redis-3.0\msvs\x64\Debug Release:redis-3.0\msvs\x64\Release

Input / Additional Dependencies: hiredis.lib Win32_Interop.lib

Input / Ignore Specific Default Libraries:MSVCRT

发布功能实现

#include "hiredis\hiredis.h" #include "Win32_Interop\Win32_APIs.h" #include <iostream> #include <stdio.h> int _tmain(int argc, _TCHAR* argv[]) { ///1、连接Redis服务器 redisContext *context = redisConnect("127.0.0.1", 6003); if (context->err) { fprintf(stderr, "Could not connect to Redis at %s:%d: %s\n", "127.0.0.1", 6003, context->errstr); redisFree(context); context = NULL; return REDIS_ERR; } ///2、校验密码 char *auth = "123456"; redisReply *reply = (redisReply *)redisCommand(context, "AUTH %s", auth); if (NULL == reply) { return REDIS_ERR; } else { freeReplyObject(reply); } ///3、选择数据库 reply = (redisReply *)redisCommand(context, "SELECT %d", 1); if (NULL == reply) { return REDIS_ERR; } else { if (REDIS_REPLY_ERROR == reply->type) { return REDIS_ERR; } freeReplyObject(reply); } while (1) { QString strPublishMessage; char s[1024]; if (gets(s) == NULL) { continue; } strPublishMessage.append(s); if (strPublishMessage.compare("Q"), Qt::CaseInsensitive) { break; } ///4、推送消息给订阅段 reply = (redisReply *)redisCommand(context, "publish %s %s", "RedisChat", strPublishMessage.toAscii().data()); if (NULL == reply) { return REDIS_ERR; } else { freeReplyObject(reply); } } redisFree(context); context = NULL; return 0; }

订阅功能实现

#include "hiredis\hiredis.h" #include "Win32_Interop\Win32_APIs.h" #include <iostream> #include <stdio.h> int _tmain(int argc, _TCHAR* argv[]) { ///1、连接Redis服务器 redisContext *context = redisConnect("127.0.0.1", 6003); if (context->err) { fprintf(stderr, "Could not connect to Redis at %s:%d: %s\n", "127.0.0.1", 6003, context->errstr); redisFree(context); context = NULL; return REDIS_ERR; } ///2、校验密码 char *auth = "123456"; redisReply *reply = (redisReply *)redisCommand(context, "AUTH %s", auth); if (NULL == reply) { return REDIS_ERR; } else { freeReplyObject(reply); } ///3、选择数据库 reply = (redisReply *)redisCommand(context, "SELECT %d", 1); if (NULL == reply) { return REDIS_ERR; } else { if (REDIS_REPLY_ERROR == reply->type) { return REDIS_ERR; } freeReplyObject(reply); } ///4、订阅频道 reply = (redisReply *)redisCommand(context, "subscribe %s", "RedisChat"); if (NULL == reply) { return REDIS_ERR; } else { freeReplyObject(reply); } while (1) { ///5、阻塞等待 直至取得订阅消息 void *_reply; if (redisGetReply(context, &_reply) != REDIS_OK) { continue; } reply = (redisReply*)_reply; QString strMessage; for (int nIndex = 0; nIndex < reply->elements; nIndex++) { std::cout << nIndex + 1 << ")"; std::cout << reply->element[nIndex]->str << std::endl; } freeReplyObject(reply); std::cout << "---------------------------------------" << std::endl; } redisFree(context); context = NULL; return 0; }

测试

发布端依次发布3条消息:

订阅端依次收到3条消息:

转载请注明原文地址: https://www.6miu.com/read-22458.html

最新回复(0)