redisContext *m_subcribe_context;
m_subcribe_context = redisConnect("127.0.0.1", 6379);
if (m_subcribe_context == nullptr)
{
printf("connect redis failed!\n");
}
reply = (redisReply *)redisCommand(m_subcribe_context,"SUBSCRIBE topic");
freeReplyObject(reply);
int done = 0;
while (!done)
{
if (REDIS_ERR == redisBufferWrite(m_subcribe_context, &done))
{
printf( "subscribe command failed!\n");
}
}
void RedisSubscribeMsgThread()
{
redisReply *reply = nullptr;
while (REDIS_OK == redisGetReply(m_subcribe_context, (void **)&reply))
{
if (reply != nullptr && reply->element[2] != nullptr && reply->element[2]->str != nullptr)
{
printf("channel [%s] recv [%s]\n", reply->element[1]->str,reply->element[2]->str);
}
if(0 == string(reply->element[1]->str).compare("topic"))
{
if(0 == string(reply->element[2]->str).compare("msg1"))
{
}
if(0 == string(reply->element[2]->str).compare("msg2"))
{
}
if(0 == string(reply->element[2]->str).compare("msg3"))
{
}
}
freeReplyObject(reply);
}
}