コード例 #1
0
ファイル: Program.cs プロジェクト: 40a/NSQnet
        public static NSQSubscriber GetSubscriber(String shortId, String longId, String host, Int32 port, String topicName, String channelName = null)
        {
            var sub = new NSQSubscriber(shortId, longId, host, port);

            channelName = channelName ?? topicName;

            sub.Initialize();

            //closures are AWESOME and you should use them.
            Action<Object, NSQMessageEventArgs> messageHandler = (sender, e) =>
            {
                Console.Write(String.Format("{0}::{2}.{1} MSG ", host, channelName, topicName));
                Console.WriteLine(e.Message.Body);
                sub.Finish(e.Message.MessageId);
                sub.ResetReadyCount();
            };

            Action<Object, EventArgs> disconnectedHandler = (sender, e) =>
            {
                var disconnected_sub = (sender as NSQSubscriber);

                Console.WriteLine(String.Format("{0}::{2}.{1} Disconnected", host, channelName, topicName));

                NSQSubscriber cached_sub = null;
                _subscribers.TryRemove(disconnected_sub.LongIdentifier.ToLower(), out cached_sub);

                disconnected_sub.Dispose();
                cached_sub.Dispose();

                disconnected_sub = null;
                cached_sub = null;
            };

            sub.MaxReadyCount = 5;

            sub.NSQMessageRecieved += new NSQMessageRecievedHandler(messageHandler);
            sub.NSQClientDisconnected += new NSQProtocolDisconnectedHandler(disconnectedHandler);

            sub.Subscribe(topicName, channelName);
            sub.ResetReadyCount();

            Console.WriteLine(String.Format("{0}::{2}.{1} Subscribed", host, channelName, topicName));

            return sub;
        }
コード例 #2
0
        public static NSQSubscriber GetSubscriber(String shortId, String longId, String host, Int32 port, String topicName, String channelName = null)
        {
            var sub = new NSQSubscriber(shortId, longId, host, port);

            channelName = channelName ?? topicName;

            sub.Initialize();

            //closures are AWESOME and you should use them.
            Action <Object, NSQMessageEventArgs> messageHandler = (sender, e) =>
            {
                Console.Write(String.Format("{0}::{2}.{1} MSG ", host, channelName, topicName));
                Console.WriteLine(e.Message.Body);
                sub.Finish(e.Message.MessageId);
                sub.ResetReadyCount();
            };

            Action <Object, EventArgs> disconnectedHandler = (sender, e) =>
            {
                var disconnected_sub = (sender as NSQSubscriber);

                Console.WriteLine(String.Format("{0}::{2}.{1} Disconnected", host, channelName, topicName));

                NSQSubscriber cached_sub = null;
                _subscribers.TryRemove(disconnected_sub.LongIdentifier.ToLower(), out cached_sub);

                disconnected_sub.Dispose();
                cached_sub.Dispose();

                disconnected_sub = null;
                cached_sub       = null;
            };

            sub.MaxReadyCount = 5;

            sub.NSQMessageRecieved    += new NSQMessageRecievedHandler(messageHandler);
            sub.NSQClientDisconnected += new NSQProtocolDisconnectedHandler(disconnectedHandler);

            sub.Subscribe(topicName, channelName);
            sub.ResetReadyCount();

            Console.WriteLine(String.Format("{0}::{2}.{1} Subscribed", host, channelName, topicName));

            return(sub);
        }