Advertisement
codeplanner

XSockets Queue Messages for offliners

Jan 4th, 2013
2,411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.62 KB | None | 0 0
  1. //A XSockets controller that will queue messages when a client is offline.
  2. public class DemoController : XSocketController
  3. {
  4.     public string Name { get; set; }
  5.  
  6.     public DemoController()
  7.     {
  8.         this.OnClientConnect += DemoController_OnClientConnect;
  9.         this.OnClientDisConnect += DemoController_OnClientDisConnect;
  10.     }
  11.  
  12.     void DemoController_OnClientDisConnect(object sender, Core.Common.Socket.Event.Arguments.OnClientDisConnectArgs e)
  13.     {
  14.         //Tell the queue to store messages for the events 'test1' and 'test2'
  15.         QueueHelper<DemoController>.OfflineSubscribe(this, "test1", "test2");
  16.     }
  17.  
  18.     void DemoController_OnClientConnect(object sender, Core.Common.Socket.Event.Arguments.OnClientConnectArgs e)
  19.     {
  20.         //Default name to filter on in a test1 send, this can be changed by connection paramters or a regular actionmethod! We have STATE in the controllers!!!
  21.         this.Name = "Uffe";
  22.         //Get any messgaes that arrived during offline time.
  23.         QueueHelper<DemoController>.OnlinePublish(this);
  24.     }
  25.  
  26.     public void Test1(ITextArgs message)
  27.     {
  28.         //A condition, we only want to send to clients with name Uffe
  29.         Func<DemoController, bool> condition = p => p.Name == "Uffe";
  30.         //Only send to the clients matching the condition
  31.         this.SendTo<DemoController>(condition, message);
  32.         //Queue message for clients that may be offline - Also include the condition!
  33.         QueueHelper<DemoController>.Queue(this, message, condition);
  34.     }
  35.  
  36.     public void Test2(CustomMessage message)
  37.     {
  38.         //Send to everyone
  39.         this.SendToAll(message.AsTextArgs("test2"));
  40.         //Queue message for clients that may be offline
  41.         QueueHelper<DemoController>.Queue(this, message.AsTextArgs("test2"));
  42.     }
  43. }
  44.  
  45.  
  46.    
  47. //The helpers for queing and sending messages
  48. public static class QueueHelper<T> where T : IXBaseSocket
  49. {
  50.     public class MessageWrapper<T> where T : IXBaseSocket
  51.     {
  52.         public Func<T, bool> Func { get; set; }
  53.         public ITextArgs TextArgs { get; set; }
  54.  
  55.         public MessageWrapper(ITextArgs textArgs, Func<T, bool> func)
  56.         {
  57.             this.Func = func;
  58.             this.TextArgs = textArgs;
  59.         }
  60.  
  61.         public MessageWrapper(ITextArgs textArgs)
  62.         {
  63.             this.Func = null;
  64.             this.TextArgs = textArgs;
  65.         }
  66.     }
  67.     private static IDictionary<Guid, IDictionary<string, IList<MessageWrapper<T>>>> queue;
  68.  
  69.     static QueueHelper()
  70.     {
  71.         queue = new Dictionary<Guid, IDictionary<string, IList<MessageWrapper<T>>>>();
  72.     }
  73.  
  74.     public static void OfflineSubscribe(T socket, params string[] events)
  75.     {
  76.         if (!queue.ContainsKey(socket.StorageGuid))
  77.         {
  78.             queue.Add(socket.StorageGuid, new Dictionary<string, IList<MessageWrapper<T>>>());
  79.         }
  80.  
  81.         foreach (var @event in events.Where(@event => !queue[socket.StorageGuid].ContainsKey(socket.Alias + @event)))
  82.         {
  83.             queue[socket.StorageGuid].Add(socket.Alias + @event, new List<MessageWrapper<T>>());
  84.         }
  85.     }
  86.  
  87.     public static void OnlinePublish(T socket)
  88.     {
  89.  
  90.         if (queue.ContainsKey(socket.StorageGuid))
  91.         {
  92.             foreach (var message in queue[socket.StorageGuid].SelectMany(messages => messages.Value))
  93.             {
  94.                 if (message.Func != null)
  95.                 {
  96.                     if (socket.GetIXBaseSockets<T>(message.Func).Count(x => x.StorageGuid == socket.StorageGuid) > 0)
  97.                     {
  98.                         socket.Send(message.TextArgs);
  99.                     }
  100.                 }
  101.                 else
  102.                 {
  103.                     socket.Send(message.TextArgs);
  104.                 }
  105.             }
  106.             queue[socket.StorageGuid] = new Dictionary<string, IList<MessageWrapper<T>>>();
  107.         }
  108.     }
  109.  
  110.     public static void Queue(T socket, ITextArgs message, Func<T, bool> func)
  111.     {
  112.         foreach (var client in queue.Where(client => client.Value.ContainsKey(socket.Alias + message.@event)))
  113.         {
  114.             client.Value[socket.Alias + message.@event].Add(new MessageWrapper<T>(message, func));
  115.         }
  116.     }
  117.  
  118.     public static void Queue(T socket, ITextArgs message)
  119.     {
  120.         foreach (var client in queue.Where(client => client.Value.ContainsKey(socket.Alias + message.@event)))
  121.         {
  122.             client.Value[socket.Alias + message.@event].Add(new MessageWrapper<T>(message));
  123.         }
  124.     }
  125. }
  126.  
  127. //Just for showing that it also works with custom types (used in Test2 method)
  128. public class CustomMessage
  129. {
  130.     public string Message { get; set; }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement