windows c++使用hiredis同步模式实现发布订阅

void RedisSync()
{
	redisContext* rc = redisConnect("127.0.0.1", 6379);
	if (rc == NULL || rc->err)
	{
		return;
	}

	redisReply *reply = redisCommand(rc, "auth lls");
	freeReplyObject(reply);

	reply = redisCommand(rc, "subscribe luls");
	freeReplyObject(reply);
	while (redisGetReply(rc, (void **)&reply) == REDIS_OK)
	{
		if (reply == NULL)
		{
			return;
		}

		if (reply->type == REDIS_REPLY_ERROR)
		{
			printf("Error: return [REDIS_REPLY_ERROR]%s\n", reply->str);
		}
		else if (reply->type == REDIS_REPLY_STATUS)
		{
			if (stricmp(reply->str, "OK") != 0)
			{
				printf("Error: return [REDIS_REPLY_STATUS]%s\n", reply->str);
				freeReplyObject(reply);
				return;
			}
			printf("return [REDIS_REPLY_STATUS]: %d\n", reply->str);
		}
		else if (reply->type == REDIS_REPLY_INTEGER)
		{
			printf("return [REDIS_REPLY_INTEGER]: %l64d\n", reply->integer);
		}
		else if (reply->type == REDIS_REPLY_STRING)
		{
			printf("return [REDIS_REPLY_STRING]: %s\n", reply->str);
		}
		else if (reply->type == REDIS_REPLY_ARRAY)
		{
			for (int i = 0; i < reply->elements; i++)
			{
				printf("%d)%s\n", i + 1, reply->element[i]->str);
			}
			printf("\n");
		}
		else if (reply->type == REDIS_REPLY_NIL)
		{
			printf("return [REDIS_REPLY_NIL]: %s\n", reply->str);
		}
		freeReplyObject(reply);
	}
	redisFree(rc);
}

你可能感兴趣的:(windows c++使用hiredis同步模式实现发布订阅)