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

相关命令

向指定频道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条消息:
这里写图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值