我一直hiredis
作为客户工作set
,publish
除subscribe
. 当我尝试发布到我订阅的频道时,我可以订阅但无法收到任何消息。这是我正在使用的代码:
int main() {
signal(SIGPIPE, sigHandler);
struct event_base *base = event_base_new();
assert(base != nullptr);
auto redis = redisAsyncConnect(IP, PORT);
if (redis != nullptr && redis->err) {
std::cerr << redis->err;
delete redis;
exit(1);
}
std::cout << "Connected" << END;
int flag;
std::cout << "Calling redisLibeventAttach" << END;
redisLibeventAttach(redis, base);
flag = redisAsyncCommand(redis, onMessage, (char *) "sub", "SUBSCRIBE test");
std::cout << "Command ret: " << flag << END;
flag = event_base_dispatch(base);
std::cout << " Event base flag " << flag << END;
return 0;
}
和消息功能:
void onMessage(redisAsyncContext *context, void *r, void *data) {
assert(r != nullptr);
auto *reply = static_cast<redisReply *>(r);
assert(reply != nullptr);
std::cout << "Reply " << reply->type << END;
switch (reply->type) {
case REDIS_REPLY_ARRAY:
for (int i = 0; i < reply->elements; ++i) {
std::cout << i << " -- " << reply->element[i]->str << END;
}
break;
case REDIS_REPLY_STRING:
std::cout << reply->str << END;
break;
}
}
还有什么我应该做的吗?