Exemple #1
0
        /// <summary>
        /// Subscribe stream channel
        /// </summary>
        /// <param name="channel">stream channel</param>
        public void Subscribe(string channel)
        {
            //Get channel subscriber
            var subscriber = GetSubscriber();

            try {
                //Subscribe channel
                subscriber?.Subscribe(channel, (redisChannel, message) => {
                    //Convert object
                    var obj = ObserverDataUtility <T> .RedisValueToObj(message);
                    //Invoke event
                    OnReceiveMessageHadle(obj, channel);
                });

                //Unsubscribe channel
                subscriber?.Unsubscribe(channel, (redisChannel, value) => {
                    //Subscribe channel
                    Subscribe(channel);
                });
            } catch (Exception ex) {
                //Invoke event
                OnErrorMessageHadle(ex);
                //Waitting for 1 second
                Task.Run(() => { Thread.Sleep(1000); });
                //Subscribe channel
                Subscribe(channel);
            }
        }
Exemple #2
0
        /// <summary>
        /// Publish message
        /// </summary>
        /// <param name="channel">stream channel</param>
        /// <param name="obj">object</param>
        public void Publish(string channel, T obj)
        {
            //Flag
            if (!IsConnected())
            {
                //Invoke event
                OnErrorMessageHadle(new Exception("Connection is not ready to publish"));
                return;
            }

            //Get subscriber
            var publisher = GetSubscriber();
            //Get message data
            var message = ObserverDataUtility <T> .ObjToRedisValue(obj);

            //Publish
            publisher?.Publish(channel, message);
            //Invoke event
            OnSendMessageHadle(obj);
        }