예제 #1
0
 public static void Publish(Message message)
 {
     lock (((IDictionary)Subscribers).SyncRoot)
     {
         List<Guid> subscribingConnections;
         var subscription = new Subscription(message.Channel, message.Group);
         if (Subscribers.TryGetValue(subscription, out subscribingConnections))
         {
             foreach (var connection in subscribingConnections)
             {
                 AddToMessageQueue(connection, message);
             }
         }
     }
 }
예제 #2
0
        static void AddToMessageQueue(Guid connectionId, Message message)
        {
            lock (((IDictionary) MessageQueues).SyncRoot)
            {
                if (!MessageQueues.ContainsKey(connectionId))
                {
                    var buffer = new MessageQueue(20, queue =>
                    {
                        queue.Timer.Stop();
                        lock (((IDictionary)MessageQueues).SyncRoot)
                        {
                            MessageQueues.Remove(connectionId);
                            Complete(connectionId, queue.Messages);
                        }
                    });

                    MessageQueues.Add(connectionId, buffer);
                }

                MessageQueue messageQueue = MessageQueues[connectionId];
                messageQueue.Timer.Stop();
                messageQueue.Messages.Add(message);
                messageQueue.Timer.Start();
            }
        }