相关命令
向指定频道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[])
{
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;
}
char *auth =
"123456";
redisReply *reply = (redisReply *)redisCommand(context,
"AUTH %s", auth);
if (
NULL == reply)
{
return REDIS_ERR;
}
else
{
freeReplyObject(reply);
}
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;
}
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[])
{
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;
}
char *auth =
"123456";
redisReply *reply = (redisReply *)redisCommand(context,
"AUTH %s", auth);
if (NULL == reply)
{
return REDIS_ERR;
}
else
{
freeReplyObject(reply);
}
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);
}
reply = (redisReply *)redisCommand(context,
"subscribe %s",
"RedisChat");
if (NULL == reply)
{
return REDIS_ERR;
}
else
{
freeReplyObject(reply);
}
while (
1)
{
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条消息: