Start() public method

public Start ( ) : IRedisPubSubServer
return IRedisPubSubServer
        public void Execute(string ipAddress)
        {
            Manager = new RedisManagerPool(ipAddress);
            StartedAt = DateTime.UtcNow;

            var q = new Timer { Interval = 1000 };
            q.Elapsed += OnInterval;
            q.Enabled = true;

            using (PubSubServer = new RedisPubSubServer(Manager, Channel)
            {
                OnStart = () =>
                {
                    Console.WriteLine("OnStart: #" + Interlocked.Increment(ref StartCount));
                },
                OnHeartbeatSent = () =>
                {
                    Console.WriteLine("OnHeartbeatSent: #" + Interlocked.Increment(ref HeartbeatsSent));
                },
                OnHeartbeatReceived = () =>
                {
                    Console.WriteLine("OnHeartbeatReceived: #" + Interlocked.Increment(ref HeartbeatsReceived));
                },
                OnMessage = (channel, msg) =>
                {
                    Console.WriteLine("OnMessage: @" + channel + ": " + msg);
                },
                OnStop = () =>
                {
                    Console.WriteLine("OnStop: #" + Interlocked.Increment(ref StopCount));
                },
                OnError = ex =>
                {
                    Console.WriteLine("OnError: #" + Interlocked.Increment(ref ErrorCount) + " ERROR: " + ex);
                },
                OnFailover = server =>
                {
                    Console.WriteLine("OnFailover: #" + Interlocked.Increment(ref FailoverCount));
                },
                OnDispose = () =>
                {
                    Console.WriteLine("OnDispose: #" + Interlocked.Increment(ref DisposeCount));
                },
                OnUnSubscribe = channel =>
                {
                    Console.WriteLine("OnUnSubscribe: #" + Interlocked.Increment(ref UnSubscribeCount) + " channel: " + channel);
                },
            })
            {
                Console.WriteLine("PubSubServer StartedAt: " + StartedAt.ToLongTimeString());
                PubSubServer.Start();

                "Press Enter to Quit...".Print();
                Console.ReadLine();
                Console.WriteLine("PubSubServer EndedAt: " + DateTime.UtcNow.ToLongTimeString());
                Console.WriteLine("PubSubServer TimeTaken: " + (DateTime.UtcNow - StartedAt).TotalSeconds + "s");
            }
        }
Beispiel #2
0
        /// <summary>
        /// 发布
        /// </summary>
        public void Pub()
        {
            ServiceStack.Redis.IRedisClientsManager RedisClientManager = new ServiceStack.Redis.PooledRedisClientManager("127.0.0.1.65:6379");
            //发布、订阅服务 IRedisPubSubServer
            ServiceStack.Redis.RedisPubSubServer pubSubServer = new ServiceStack.Redis.RedisPubSubServer(RedisClientManager, "channel-1");

            pubSubServer.OnMessage = (channel, msg) =>
            {
                Console.WriteLine(string.Format("服务端:频道{0}接收消息:{1}    时间:{2}", channel, msg, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")));
                Console.WriteLine("___________________________________________________________________");
            };
            pubSubServer.OnStart = () =>
            {
                Console.WriteLine("发布服务已启动");
                Console.WriteLine("___________________________________________________________________");
            };
            pubSubServer.OnStop        = () => { Console.WriteLine("服务停止"); };
            pubSubServer.OnUnSubscribe = (channel) => { Console.WriteLine(channel); };
            pubSubServer.OnError       = (e) => { Console.WriteLine(e.Message); };
            pubSubServer.OnFailover    = (s) => { Console.WriteLine(s); };
            pubSubServer.Start();
        }
 public void Publish()
 {
     try
     {
         ServiceStack.Redis.IRedisClientsManager RedisClientManager = new RedisClientConfig().GetRedisClientManager();
         //发布、订阅服务 IRedisPubSubServer
         ServiceStack.Redis.IRedisPubSubServer pubSubServer = new ServiceStack.Redis.RedisPubSubServer(RedisClientManager, _channelName);
         //接收消息事件
         pubSubServer.OnMessage = (channel, msg) =>
         {
             if (_onPublicMessage != null)
             {
                 _onPublicMessage(channel, msg, _mainHttpApplication);
             }
         };
         //启动服务事件处理事件
         pubSubServer.OnStart = () =>
         {
             if (_onPublicStart != null)
             {
                 _onPublicStart();
             }
         };
         //停止服务事件处理事件
         pubSubServer.OnStop = () =>
         {
             if (_onPublicStop != null)
             {
                 _onPublicStop();
             }
         };
         //取消发布处理事件
         pubSubServer.OnUnSubscribe = (channel) =>
         {
             if (_onPublicUnSubscribe != null)
             {
                 _onPublicUnSubscribe(channel, _mainHttpApplication);
             }
         };
         //发布报错时处理事件
         pubSubServer.OnError = (e) =>
         {
             if (_onPublicError != null)
             {
                 _onPublicError(e);
             }
         };
         //发布故障时处理事件
         pubSubServer.OnFailover = (s) =>
         {
             if (_onPublicFailover != null)
             {
                 _onPublicFailover(s);
             }
         };
         //启动发布服务
         pubSubServer.Start();
     }
     catch (Exception ex)
     {
         //Log.Error("EasyFrameRedis", "Publish", ex.Message);
     }
 }