Simulation Core
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

51 lines
1.2 KiB

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <signal.h>
  5. #include "hiredis.h"
  6. #include "async.h"
  7. #include "adapters/libevent.h"
  8. #include <unistd.h>
  9. void onMessage(redisAsyncContext * c, void *reply, void * privdata) {
  10. int j;
  11. redisReply * r = reply;
  12. if (reply == NULL) return;
  13. // printf("got a message of type: %i\n", r->type);
  14. if (r->type == REDIS_REPLY_ARRAY && r->elements==3) {
  15. if(strcmp(r->element[0]->str,"message")==0 && strcmp(r->element[1]->str,"f7e58e26-f9c0-44e7-8954-08dc093dd85d.ch")==0)
  16. {
  17. printf("message: %s\n",r->element[2]->str);
  18. }
  19. }
  20. }
  21. int main(int argc, char ** argv) {
  22. // printf("Hello");
  23. signal(SIGPIPE, SIG_IGN);
  24. struct event_base * base = event_base_new();
  25. redisAsyncContext * c = redisAsyncConnect("127.0.0.1", 6379);
  26. if (c->err) {
  27. printf("error: %s\n", c->errstr);
  28. return 1;
  29. }
  30. redisAsyncCommand(c, NULL,NULL,"AUTH %s", "1qazxsw2$$");
  31. redisLibeventAttach(c, base);
  32. redisAsyncCommand(c, onMessage, NULL, "SUBSCRIBE f7e58e26-f9c0-44e7-8954-08dc093dd85d.ch");
  33. event_base_dispatch(base);
  34. for(int i=0;i<10;i++)
  35. {
  36. sleep(1);
  37. printf("i=%d\n",i);
  38. }
  39. return 0;
  40. }