您无需告诉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: %sn", (char*)priv, reply->element[1]->str, reply->element[2]->str ); } }}void connectCallback(const redisAsyncContext *c, int status) { if (status != REDIS_OK) { printf("Error: %sn", c->errstr); return; } printf("Connected...n");}void disconnectCallback(const redisAsyncContext *c, int status) { if (status != REDIS_OK) { printf("Error: %sn", 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) { printf("Error: %sn", 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订阅的函数。



