关于hiredis的libev异步实现参考 https://blog.csdn.net/fangfanglovezhou/article/details/104984849
subscribe/publish功能介绍参考 https://blog.csdn.net/fangfanglovezhou/article/details/105248751
直接上代码,客户端1实现subscribe功能:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <hiredis/hiredis.h>
#include <hiredis/async.h>
#include <hiredis/adapters/libev.h>
void getCallback(redisAsyncContext *c, void *r, void *privdata) {
redisReply *reply = r;
if (reply == NULL) return;
printf("%s\n",(char*)privdata);
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*)privdata,
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);
redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379);
if (c->err) {
/* Let *c leak for now... */
printf("Error: %s\n", c->errstr);
return 1;
}
redisLibevAttach(EV_DEFAULT_ c);
redisAsyncSetConnectCallback(c,connectCallback);
redisAsyncSetDisconnectCallback(c,disconnectCallback);
redisAsyncCommand(c, NULL, NULL, "auth 123");
redisAsyncCommand(c,getCallback, (char*)"send message","subscribe test1");
ev_loop(EV_DEFAULT_ 0);
return 0;
}
客户端2代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <hiredis/hiredis.h>
#include <hiredis/async.h>
#include <hiredis/adapters/libev.h>
void getCallback(redisAsyncContext *c, void *r, void *privdata) {
redisReply *reply = r;
if (reply == NULL) return;
printf("argv[%s]: %s\n", (char*)privdata, reply->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);
redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379);
if (c->err) {
/* Let *c leak for now... */
printf("Error: %s\n", c->errstr);
return 1;
}
redisLibevAttach(EV_DEFAULT_ c);
redisAsyncSetConnectCallback(c,connectCallback);
redisAsyncSetDisconnectCallback(c,disconnectCallback);
redisAsyncCommand(c, NULL, NULL, "AUTH 123");
redisAsyncCommand(c, getCallback, (char*)"send message", "publish test1 123");
ev_loop(EV_DEFAULT_ 0);
return 0;
}
首先,启动客户端1,客户端1会打印消息:
可以看到,在连接服务器后,会收到服务器的回复,调用回调函数getCallback,但没有发布消息,因此指挥打印发送过去的消息。
随后,从新打开一个窗口,启动客户端2,客户端1会打印消息:
可以看到,此时客户端2向频道test1发布消息,服务器收到后,会向监控通道test1的所有客户端发送消息,随后客户端1收到服务端发到通道test1消息。