복붙노트

[REDIS] Hiredis는 메시지를 대기

REDIS

Hiredis는 메시지를 대기

나는 레디 스 서버에 연결 hiredis C 라이브러리를 사용하고 있습니다. 나는 새 메시지에 가입 한 후 새 메시지를 기다리는 방법을 알아낼 수 없습니다입니다.

내 코드 조회와 같은 :

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 for 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");
  redisAsyncCommand(c, getCallback, (char*) "end-1", "SUBSCRIBE foo");

이제 어떻게 채널에 메시지를 기다리는 hiredis에게?

해결법

  1. ==============================

    1.이벤트 루프는 단지 이전에 등록 된 레디 스 연결을 기다립니다 : 당신은 당신이 채널에서 기다릴 필요가 hiredis 말할 필요가 없습니다.

    이벤트 루프는 단지 이전에 등록 된 레디 스 연결을 기다립니다 : 당신은 당신이 채널에서 기다릴 필요가 hiredis 말할 필요가 없습니다.

    여기에 완벽한 예입니다 :

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <signal.h>
    #include "hiredis.h"
    #include "async.h"
    #include "adapters/libevent.h"
    
    void subCallback(redisAsyncContext *c, void *r, void *priv) {
        redisReply *reply = r;
        if (reply == NULL) return;
        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*)priv,
                        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);
        struct event_base *base = event_base_new();
    
        redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379);
        if (c->err) {
            /* Let *c leak for now... */
            printf("Error: %s\n", c->errstr);
            return 1;
        }
    
        redisLibeventAttach(c,base);
        redisAsyncSetConnectCallback(c,connectCallback);
        redisAsyncSetDisconnectCallback(c,disconnectCallback);
        redisAsyncCommand(c, subCallback, (char*) "sub", "SUBSCRIBE foo");
    
        event_base_dispatch(base);
        return 0;
    }
    

    그냥 뭔가를 게시하려면 다음 명령을 사용하여 당신은 그것을 테스트 할 수 있습니다 :

    redis-cli publish foo something
    

    event_base_dispatch 기능은 실제로 이벤트 루프를 시작하는 하나이며, 그것은 레디 스 가입 기다려야합니다.

  2. from https://stackoverflow.com/questions/16213676/hiredis-waiting-for-message by cc-by-sa and MIT license