Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //A XSockets controller that will queue messages when a client is offline.
- public class DemoController : XSocketController
- {
- public string Name { get; set; }
- public DemoController()
- {
- this.OnClientConnect += DemoController_OnClientConnect;
- this.OnClientDisConnect += DemoController_OnClientDisConnect;
- }
- void DemoController_OnClientDisConnect(object sender, Core.Common.Socket.Event.Arguments.OnClientDisConnectArgs e)
- {
- //Tell the queue to store messages for the events 'test1' and 'test2'
- QueueHelper<DemoController>.OfflineSubscribe(this, "test1", "test2");
- }
- void DemoController_OnClientConnect(object sender, Core.Common.Socket.Event.Arguments.OnClientConnectArgs e)
- {
- //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!!!
- this.Name = "Uffe";
- //Get any messgaes that arrived during offline time.
- QueueHelper<DemoController>.OnlinePublish(this);
- }
- public void Test1(ITextArgs message)
- {
- //A condition, we only want to send to clients with name Uffe
- Func<DemoController, bool> condition = p => p.Name == "Uffe";
- //Only send to the clients matching the condition
- this.SendTo<DemoController>(condition, message);
- //Queue message for clients that may be offline - Also include the condition!
- QueueHelper<DemoController>.Queue(this, message, condition);
- }
- public void Test2(CustomMessage message)
- {
- //Send to everyone
- this.SendToAll(message.AsTextArgs("test2"));
- //Queue message for clients that may be offline
- QueueHelper<DemoController>.Queue(this, message.AsTextArgs("test2"));
- }
- }
- //The helpers for queing and sending messages
- public static class QueueHelper<T> where T : IXBaseSocket
- {
- public class MessageWrapper<T> where T : IXBaseSocket
- {
- public Func<T, bool> Func { get; set; }
- public ITextArgs TextArgs { get; set; }
- public MessageWrapper(ITextArgs textArgs, Func<T, bool> func)
- {
- this.Func = func;
- this.TextArgs = textArgs;
- }
- public MessageWrapper(ITextArgs textArgs)
- {
- this.Func = null;
- this.TextArgs = textArgs;
- }
- }
- private static IDictionary<Guid, IDictionary<string, IList<MessageWrapper<T>>>> queue;
- static QueueHelper()
- {
- queue = new Dictionary<Guid, IDictionary<string, IList<MessageWrapper<T>>>>();
- }
- public static void OfflineSubscribe(T socket, params string[] events)
- {
- if (!queue.ContainsKey(socket.StorageGuid))
- {
- queue.Add(socket.StorageGuid, new Dictionary<string, IList<MessageWrapper<T>>>());
- }
- foreach (var @event in events.Where(@event => !queue[socket.StorageGuid].ContainsKey(socket.Alias + @event)))
- {
- queue[socket.StorageGuid].Add(socket.Alias + @event, new List<MessageWrapper<T>>());
- }
- }
- public static void OnlinePublish(T socket)
- {
- if (queue.ContainsKey(socket.StorageGuid))
- {
- foreach (var message in queue[socket.StorageGuid].SelectMany(messages => messages.Value))
- {
- if (message.Func != null)
- {
- if (socket.GetIXBaseSockets<T>(message.Func).Count(x => x.StorageGuid == socket.StorageGuid) > 0)
- {
- socket.Send(message.TextArgs);
- }
- }
- else
- {
- socket.Send(message.TextArgs);
- }
- }
- queue[socket.StorageGuid] = new Dictionary<string, IList<MessageWrapper<T>>>();
- }
- }
- public static void Queue(T socket, ITextArgs message, Func<T, bool> func)
- {
- foreach (var client in queue.Where(client => client.Value.ContainsKey(socket.Alias + message.@event)))
- {
- client.Value[socket.Alias + message.@event].Add(new MessageWrapper<T>(message, func));
- }
- }
- public static void Queue(T socket, ITextArgs message)
- {
- foreach (var client in queue.Where(client => client.Value.ContainsKey(socket.Alias + message.@event)))
- {
- client.Value[socket.Alias + message.@event].Add(new MessageWrapper<T>(message));
- }
- }
- }
- //Just for showing that it also works with custom types (used in Test2 method)
- public class CustomMessage
- {
- public string Message { get; set; }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement