Esempio n. 1
0
        /// <summary>
        /// 订阅
        /// </summary>
        public void Subscription()
        {
            using (ServiceStack.Redis.RedisClient consumer = new RedisClient("127.0.0.1", 6379))
            {
                //创建订阅
                ServiceStack.Redis.IRedisSubscription subscription = consumer.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("订阅客户端:取消订阅"); };

                ////模拟:发送100条消息给服务
                //Task.Run(() =>
                //{
                //    using (ServiceStack.Redis.IRedisClient publisher = new ServiceStack.Redis.RedisClient("127.0.0.1", 6379))
                //    {
                //        for (int i = 1; i <= 100; i++)
                //        {
                //            publisher.PublishMessage("channel-1", string.Format("这是我发送的第{0}消息!", i));

                //            System.Threading.Thread.Sleep(200);
                //        }
                //    }
                //    subscription.UnSubscribeFromAllChannels();
                //});

                //订阅频道
                subscription.SubscribeToChannels("channel-1");
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 订阅消息
        /// </summary>
        public static void Subscription(string tochannel, Action <string, string> action)
        {
            using (IRedisClient consumer = PooleClient.GetClient())
            {
                //创建订阅
                ServiceStack.Redis.IRedisSubscription subscription = consumer.CreateSubscription();

                //接收消息处理Action
                subscription.OnMessage = (channel, msg) =>
                {
                    if (action != null)
                    {
                        action(channel, msg);
                    }
                };

                //订阅频道
                subscription.SubscribeToChannels(tochannel);
            }
        }
        public void Subscription()
        {
            try
            {
                //创建订阅
                ServiceStack.Redis.IRedisSubscription subscription = redisClient.CreateSubscription();
                //接收消息处理Action
                subscription.OnMessage = (changel, msg) =>
                {
                    if (_onSubscriptionMessage != null)
                    {
                        _onSubscriptionMessage(changel, msg, _mainHttpApplication);
                    }
                };

                //订阅事件处理
                subscription.OnSubscribe = (channel) =>
                {
                    if (_onSubscribe != null)
                    {
                        _onSubscribe(channel, _mainHttpApplication);
                    }
                };

                //取消订阅事件处理
                subscription.OnUnSubscribe = (channel) =>
                {
                    if (_onUnSubscribe != null)
                    {
                        _onUnSubscribe(channel, _mainHttpApplication);
                    }
                };

                //订阅频道
                subscription.SubscribeToChannels(_channelName);
            }
            catch (Exception ex)
            {
                //Log.Error("EasyFrameRedis", "Subscription", ex.Message);
            }
        }