Enyim.Membase.MessageStreamListener.Subscribe C# (CSharp) Method

Subscribe() public method

public Subscribe ( Action callback ) : void
callback Action
return void
		public void Subscribe(Action<string> callback)
		{
			if (this.hasMessage)
				callback(this.lastMessage);

			this.MessageReceived += callback;
		}

Usage Example

        /// <summary>
        /// Returns a MessageStreamListener instance based on this instance's configuratino (timeout, bucket name etc.)
        ///
        /// When multiple listeners are requested with the exact same parameters (usually when multiple clients are instantiated from the same configuration),
        /// the same listener will be returned each time.
        /// </summary>
        /// <returns></returns>
        private MessageStreamListener GetPooledListener()
        {
            // create a unique key based on the parameters
            // to find out if we already have a listener attached to this sockIOPool
            var hcc = new HashCodeCombiner();

            hcc.Add(this.Timeout);
            hcc.Add(this.DeadTimeout);
            hcc.Add(this.RetryCount);
            hcc.Add(this.RetryTimeout.GetHashCode());
            hcc.Add(this.bucketName.GetHashCode());

            if (credential != null)
            {
                hcc.Add((this.credential.UserName ?? String.Empty).GetHashCode());
                hcc.Add((this.credential.Password ?? String.Empty).GetHashCode());
                hcc.Add((this.credential.Domain ?? String.Empty).GetHashCode());
            }

            for (var i = 0; i < this.poolUrls.Length; i++)
            {
                hcc.Add(this.poolUrls[i].GetHashCode());
            }

            var hash = hcc.CurrentHash;

            MessageStreamListener retval;

            lock (ListenerSync)
                if (listeners.TryGetValue(hash, out retval))
                {
                    listenerRefs[retval].RefCount++;
                    retval.Subscribe(this.HandleMessage);
                }
                else
                {
                    var name = this.bucketName;

                    // create a new listener for the sockIOPool urls
                    retval = new MessageStreamListener(poolUrls, (client, root) => ResolveBucketUri(client, root, name));

                    retval.Timeout      = this.Timeout;
                    retval.DeadTimeout  = this.DeadTimeout;
                    retval.Credentials  = this.credential;
                    retval.RetryCount   = this.RetryCount;
                    retval.RetryTimeout = this.RetryTimeout;

                    retval.Subscribe(this.HandleMessage);

                    listeners[hash]      = retval;
                    listenerRefs[retval] = new ListenerInfo {
                        RefCount = 1, HashKey = hash
                    };

                    retval.Start();
                }

            return(retval);
        }
All Usage Examples Of Enyim.Membase.MessageStreamListener::Subscribe