c++ - How to publish asynchronously with HiRedis -
i using hiredis c/c++ program , have written tests verify subscriptions work (i based solution on this comment).
however, can publish manually typing publish foo "abcd"
redis-cli
terminal. works per linked comment, i'd publish from c++ program. how can this?
i have tried command:
redisasynccommand(c, subcallback, (char*)"command", "publish foo \"abcd\"");
but results in runtime error:
error: err (p)subscribe / (p)unsubscribe / quit allowed in context
how can publish data within hiredis?
from https://github.com/redis/hiredis
in hiredis/examples/example-libevent.c
:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <signal.h> #include <hiredis.h> #include <async.h> #include <adapters/libevent.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); /* disconnect after receiving reply */ redisasyncdisconnect(c); } 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 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"); event_base_dispatch(base); return 0; }
Comments
Post a Comment