3

我正在使用hiredis C 库连接到redis 服务器。订阅新消息后,我无法弄清楚如何等待新消息。

我的代码如下所示:

signal(SIGPIPE, SIG_IGN );
  struct event_base *base = event_base_new();

  redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379);
  if (c->err) {
    /* Let *c leak for now... */
    printf("Error: %s\n", c->errstr);
    return 1;
  }

  redisLibeventAttach(c, base);
  redisAsyncSetConnectCallback(c, connectCallback);
  redisAsyncSetDisconnectCallback(c, disconnectCallback);
  redisAsyncCommand(c, NULL, NULL, "SET key %b", argv[argc - 1],
                    strlen(argv[argc - 1]));
  redisAsyncCommand(c, getCallback, (char*) "end-1", "GET key");
  redisAsyncCommand(c, getCallback, (char*) "end-1", "SUBSCRIBE foo");

现在如何告诉hiredis等待频道上的消息?

4

2 回答 2

8

你不必告诉hiredis你需要在频道上等待:事件循环只会等待之前注册的Redis连接。

这是一个完整的例子:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include "hiredis.h"
#include "async.h"
#include "adapters/libevent.h"

void subCallback(redisAsyncContext *c, void *r, void *priv) {
    redisReply *reply = r;
    if (reply == NULL) return;
    if ( reply->type == REDIS_REPLY_ARRAY && reply->elements == 3 ) {
        if ( strcmp( reply->element[0]->str, "subscribe" ) != 0 ) {
            printf( "Received[%s] channel %s: %s\n",
                    (char*)priv,
                    reply->element[1]->str,
                    reply->element[2]->str );
        }
    }
}

void connectCallback(const redisAsyncContext *c, int status) {
    if (status != REDIS_OK) {
        printf("Error: %s\n", c->errstr);
        return;
    }
    printf("Connected...\n");
}

void disconnectCallback(const redisAsyncContext *c, int status) {
    if (status != REDIS_OK) {
        printf("Error: %s\n", c->errstr);
        return;
    }
    printf("Disconnected...\n");
}

int main (int argc, char **argv) {
    signal(SIGPIPE, SIG_IGN);
    struct event_base *base = event_base_new();

    redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379);
    if (c->err) {
        /* Let *c leak for now... */
        printf("Error: %s\n", c->errstr);
        return 1;
    }

    redisLibeventAttach(c,base);
    redisAsyncSetConnectCallback(c,connectCallback);
    redisAsyncSetDisconnectCallback(c,disconnectCallback);
    redisAsyncCommand(c, subCallback, (char*) "sub", "SUBSCRIBE foo");

    event_base_dispatch(base);
    return 0;
}

您只需使用以下命令发布内容即可对其进行测试:

redis-cli publish foo something

event_base_dispatch 函数是实际启动事件循环并使其等待 Redis 订阅的函数。

于 2013-04-25T13:24:07.063 回答
0

基于 LibUV 的解决方案

Redis 版本:5.0.5

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include "hiredis.h"
#include "async.h"
#include <time.h>
#include <uv.h>
#include "adapters/libevent.h"
#include "adapters/libuv.h"

void subCallback(redisAsyncContext *c, void *r, void *priv) {
    redisReply *reply = r;
    time_t seconds;
    seconds = time(NULL);
    if (reply == NULL) return;
    if (reply->type == REDIS_REPLY_ARRAY && reply->elements == 3) {
        if (strcmp(reply->element[0]->str, "subscribe") != 0) {
            printf("[%d] Received[%s] channel %s: %s\n",
                   seconds,
                    (char *) priv,
                   reply->element[1]->str,
                   reply->element[2]->str);
        }
    }
}

void connectCallback(const redisAsyncContext *c, int status) {
    if (status != REDIS_OK) {
        printf("Error: %s\n", c->errstr);
        return;
    }
    printf("Connected...\n");
}

void disconnectCallback(const redisAsyncContext *c, int status) {
    if (status != REDIS_OK) {
        printf("Error: %s\n", c->errstr);
        return;
    }
    printf("Disconnected...\n");
}

int main(int argc, char **argv) {
    signal(SIGPIPE, SIG_IGN);
    uv_loop_t *loop = malloc(sizeof(uv_loop_t));
    uv_loop_init(loop);

    redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379);
    if (c->err) {
        /* Let *c leak for now... */
        printf("Error: %s\n", c->errstr);
        return 1;
    }

    redisLibuvAttach(c, loop);
    redisAsyncSetConnectCallback(c, connectCallback);
    redisAsyncSetDisconnectCallback(c, disconnectCallback);
    redisAsyncCommand(c, subCallback, (char *) "sub", "SUBSCRIBE test-channel");

    uv_run(loop, UV_RUN_DEFAULT);
    uv_loop_close(loop);
    free(loop);
    return 0;
}

频道发布

频道订阅

于 2021-02-09T03:12:57.873 回答