|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- #include "hiredis.h"
-
- void dumpReply(redisReply *r, int indent) {
- sds buffer = sdsempty();
-
- switch (r->type) {
- case REDIS_REPLY_STRING:
- buffer = sdscatfmt(buffer, "(string) %s\n", r->str);
- break;
- case REDIS_REPLY_STATUS:
- buffer = sdscatfmt(buffer, "(status) %s\n", r->str);
- break;
- case REDIS_REPLY_INTEGER:
- buffer = sdscatlen(buffer, "(integer) %lld\n", r->integer);
- break;
- case REDIS_REPLY_NIL:
- buffer = sdscatlen(buffer, "(nill)\n", 7);
- break;
- case REDIS_REPLY_ERROR:
- buffer = sdscatfmt(buffer, " (error) %s", r->str);
- break;
- case REDIS_REPLY_ARRAY:
- for (size_t i = 0; i < r->elements; i++) {
- dumpReply(r->element[i], indent + 2);
- }
- break;
- default:
- return;
- // dbg_abort("Don't know how to deal with reply type %d", r->type);
- }
-
- if (sdslen(buffer) > 0) {
- for (int i = 0; i < indent; i++)
- printf(" ");
-
- printf("%s", buffer);
- }
-
- sdsfree(buffer);
- }
- int main() {
- redisContext *c;
- redisReply *reply;
-
- // Connect to Redis server
- c = redisConnect("127.0.0.1", 6379);
- if (c->err) {
- printf("Error 0: %s\n", c->errstr);
- return 1;
- }
- // Authenticate if necessary
- reply = redisCommand(c, "AUTH %s", "1qazxsw2$$");
- freeReplyObject(reply);
-
- // Read from the stream
- char commandStr[100] = "XREADGROUP GROUP sampleGroup NOACK STREAMS sampleStream >";
- printf("command: %s\n",commandStr);
- reply = redisCommand(c, commandStr);
- printf("reply->type =>%d\n",reply->type);
- dumpReply(reply,0);
- // if (reply->type == REDIS_REPLY_ARRAY) {
- // printf("reply->elements: %ld\n",reply->elements);
- // for(int i = 0; i < reply->elements; i++) {
- // redisReply *element = reply->element[i];
- // printf("read from redis: %s\n", reply->str);
- // }
- // }
-
- // Don't forget to free the reply object when you're done with it
- freeReplyObject(reply);
-
- // Disconnect from the Redis server
- redisFree(c);
-
- return 0;
- }
|