private void OnMessage(string channel, string message) { if (_closing) { _subscription.UnSubscribeFromAllChannels(); } if (channel == Heartbeat) { return; } var args = new ObjectEventArgs(message); if (channel == AddedChannel) { _parent.OnAdded(this._parent, args); } else if (channel == ChangedChannel) { _parent.OnChanged(this._parent, args); } else if (channel == DeletedChannel) { _parent.OnDeleted(this._parent, args); } else { //TODO: Inject logger here //Logger.Instance.Error("Missing handler for redis channel " + channel); } }
public void UnSubscription(string channelName) { using (IRedisClient Redis = _prcm.GetClient()) { _redisSubscription?.UnSubscribeFromAllChannels(); _redisSubscription = null; } }
protected virtual void Dispose(bool disposing) { if (disposing) { _subscription.UnSubscribeFromAllChannels(); _subscription.Dispose(); _consumer.Dispose(); } }
protected void OnChannelMessage(string channel, string msg) { if (_closing == 1) { _redisSubscription.UnSubscribeFromAllChannels(); } if (channel.Equals(Heartbeat, StringComparison.OrdinalIgnoreCase)) { return; } else if (channel.Equals(MainChannel, StringComparison.OrdinalIgnoreCase)) { var evnt = msg.DeserializeFromJSON <TEvnt>(); System.Threading.Interlocked.Increment(ref _MessageReceived); OnEventReceived(evnt); } }
/// <summary> /// 消息事件 /// </summary> /// <param name="channel"></param> /// <param name="msg"></param> private void OnMessage(string channel, string msg) { ArraySegment <byte> buffer = new ArraySegment <byte>(new byte[2048]); buffer = new ArraySegment <byte>(Encoding.UTF8.GetBytes(msg)); try { if (channel.ToLower() == _socketid && msg == "1") { Debug.WriteLine("TCP保活----------------------------"); return; } if (_webSocket != null && _webSocket.State == WebSocketState.Open) { _webSocket.SendAsync(buffer, WebSocketMessageType.Text, true, _cancellationToken); } } catch (ObjectDisposedException ex) { _redisClient.Dispose(); _redisSubscription.Dispose(); ClearTimer(); LogManager.DefaultLogger.ErrorFormat($"异常订阅线程ID:{Thread.CurrentThread.ManagedThreadId.ToString()};对象已释放:errormsg:{ex.Message}"); } catch (Exception ex) { _redisClient.Dispose(); _redisSubscription.Dispose(); ClearTimer(); LogManager.DefaultLogger.ErrorFormat($"异常订阅线程ID:{Thread.CurrentThread.ManagedThreadId.ToString()};channel:{channel};errormsg:{ex.Message}"); } finally { if (channel.ToLower() == _socketid && msg == "stop") { _redisSubscription.UnSubscribeFromAllChannels(); //解除封锁线程 _redisClient.Dispose(); _redisSubscription.Dispose(); } } }
public void Dispose(bool disposing) { if (!this._isDisposed) { if (disposing) { if (_redisSubscription != null) { _redisSubscription.UnSubscribeFromAllChannels(); _redisSubscription.Dispose(); } if (_redisClient != null) { _redisClient.UnWatch(); _redisClient.Dispose(); } } } _isDisposed = true; }
private static void Start(string name, Action <string, string> handleAction) { using (var client = RedisManager.GetClient()) { //LogHelper.Info("创建频道连接",OpType.System); Subscription = client.CreateSubscription(); //Subscription.OnUnSubscribe = channel => // LogHelper.Info("停止监听: " + channel, OpType.System); Subscription.OnMessage = (channel, msg) => { if (msg == "STOP") { //LogHelper.Info("断开所有监听频道...",OpType.System); Subscription.UnSubscribeFromAllChannels(); //Un block thread. return; } handleAction(channel, msg); }; //LogHelper.Info("开始监听:" + name,OpType.System); Subscription.SubscribeToChannels(name); } }
private void StartListen() { using (var redisConsumer = new RedisClient(_host, _port)) { try { _redisSubscription = redisConsumer.CreateSubscription(); _redisSubscription.OnSubscribe = OnChannelSubscribe; _redisSubscription.OnUnSubscribe = OnChannelUnsubscribe; _redisSubscription.OnMessage = OnChannelMessage; System.Diagnostics.Debug.WriteLine("Before calling SubscribeToChannels"); _redisSubscription.SubscribeToChannels(MainChannel, Heartbeat); _redisSubscription.OnSubscribe = null; _redisSubscription.OnUnSubscribe = null; _redisSubscription.OnMessage = null; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); if (_redisSubscription != null) { _redisSubscription.UnSubscribeFromAllChannels(); } throw; } finally { _redisSubscription.Dispose(); _redisSubscription = null; } } }