订阅:
class Program { //版本2:使用Redis的客户端管理器(对象池) public static IRedisClientsManager redisClientManager = new PooledRedisClientManager(new string[] { //如果是Redis集群则配置多个{IP地址:端口号}即可 //例如: "10.0.0.1:6379","10.0.0.2:6379","10.0.0.3:6379" "127.0.0.1:6379" }); //从池中获取Redis客户端实例 public static IRedisClient redisClient = redisClientManager.GetClient(); static void Main(string[] args) { //创建订阅 IRedisSubscription subscription = redisClient.CreateSubscription(); //接收消息处理Action subscription.OnMessage = (channel, msg) => { Console.WriteLine("频道【" + channel + "】订阅客户端接收消息:" + ":" + msg + " [" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + "]"); Console.WriteLine("订阅数:" + subscription.SubscriptionCount); Console.WriteLine("___________________________________________________________________"); }; //订阅事件处理 subscription.OnSubscribe = (channel) => { Console.WriteLine("订阅客户端:开始订阅" + channel); }; //取消订阅事件处理 subscription.OnUnSubscribe = (a) => { Console.WriteLine("订阅客户端:取消订阅"); }; //订阅频道 subscription.SubscribeToChannels("channel-2"); Console.WriteLine("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); Console.ReadKey(); } }
发布:
//版本2:使用Redis的客户端管理器(对象池) public static IRedisClientsManager redisClientManager = new PooledRedisClientManager(new string[] { //如果是Redis集群则配置多个{IP地址:端口号}即可 //例如: "10.0.0.1:6379","10.0.0.2:6379","10.0.0.3:6379" "127.0.0.1:6379" }); //从池中获取Redis客户端实例 public static IRedisClient redisClient = redisClientManager.GetClient(); static void Main(string[] args) { while (true) { string input = Console.ReadLine(); redisClient.PublishMessage("channel-1", input); redisClient.PublishMessage("channel-2", input); } Console.ReadKey();}