- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <signal.h>
- #include "hiredis.h"
- #include "async.h"
- #include "adapters/libevent.h"
- #include <unistd.h>
-
- void onMessage(redisAsyncContext * c, void *reply, void * privdata) {
- int j;
- redisReply * r = reply;
- if (reply == NULL) return;
-
- // printf("got a message of type: %i\n", r->type);
-
- if (r->type == REDIS_REPLY_ARRAY && r->elements==3) {
- if(strcmp(r->element[0]->str,"message")==0 && strcmp(r->element[1]->str,"f7e58e26-f9c0-44e7-8954-08dc093dd85d.ch")==0)
- {
- printf("message: %s\n",r->element[2]->str);
- }
- }
- }
-
-
- int main(int argc, char ** argv) {
- // printf("Hello");
- signal(SIGPIPE, SIG_IGN);
- struct event_base * base = event_base_new();
-
- redisAsyncContext * c = redisAsyncConnect("127.0.0.1", 6379);
- if (c->err) {
- printf("error: %s\n", c->errstr);
- return 1;
- }
-
-
- redisAsyncCommand(c, NULL,NULL,"AUTH %s", "1qazxsw2$$");
- redisLibeventAttach(c, base);
- redisAsyncCommand(c, onMessage, NULL, "SUBSCRIBE f7e58e26-f9c0-44e7-8954-08dc093dd85d.ch");
- event_base_dispatch(base);
-
- for(int i=0;i<10;i++)
- {
- sleep(1);
- printf("i=%d\n",i);
- }
-
- return 0;
- }
|