Esempio n. 1
0
        /// <summary>
        /// Listen for messages published to the given channels
        /// </summary>
        /// <param name="callback">Callback for received messages on the specified channels</param>
        /// <param name="channels">Channels to subscribe</param>
        public void Subscribe(Action <RedisSubscriptionMessage> callback, params string[] channels)
        {
            if (callback != null)
            {
                AddCallback(callback, channels);
            }
            var cmd = RedisCommand.Subscribe(channels);

            _connection.Write(cmd.Command, cmd.Arguments);
        }
Esempio n. 2
0
        public void HandleSubscription(RedisSubscription command)
        {
            _connection.Write(command.Command, command.Arguments);
            if (!IsSubscribed)
            {
                using (new ActivityTracer("Handle subscriptions"))
                {
                    IsSubscribed = true;
                    while (true)
                    {
                        var resp = _connection.Read(command.Parser);
                        switch (resp.Type)
                        {
                        case RedisSubscriptionResponseType.Subscribe:
                        case RedisSubscriptionResponseType.PSubscribe:
                        case RedisSubscriptionResponseType.Unsubscribe:
                        case RedisSubscriptionResponseType.PUnsubscribe:
                            RedisSubscriptionChannel channel = resp as RedisSubscriptionChannel;
                            Count = channel.Count;
                            if (SubscriptionChanged != null)
                            {
                                SubscriptionChanged(this, new RedisSubscriptionChangedEventArgs(channel));
                            }
                            break;

                        case RedisSubscriptionResponseType.Message:
                        case RedisSubscriptionResponseType.PMessage:
                            RedisSubscriptionMessage message = resp as RedisSubscriptionMessage;
                            if (SubscriptionReceived != null)
                            {
                                SubscriptionReceived(this, new RedisSubscriptionReceivedEventArgs(message));
                            }
                            break;
                        }
                        if (Count == 0)
                        {
                            break;
                        }
                    }
                    IsSubscribed = false;
                }
            }
        }