using Longbow.Cache; using Longbow.Web.WebSockets; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Text; namespace Bootstrap.DataAccess { /// /// /// public static class NotificationHelper { /// /// /// private const string PullNotificationsIntervalDataKey = "NotificationHelper-PullNotificationsInterval"; private static readonly List MessagePool = new List(); /// /// /// /// /// public static void PushMessage(MessageBody message) { MessagePool.Add(message); CacheManager.Clear(PullNotificationsIntervalDataKey); // websocket message push WebSocketServerManager.SendAsync(new ArraySegment(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new MessageBody[] { message })))); } /// /// /// /// public static IEnumerable RetrieveMessages() { return CacheManager.GetOrAdd(PullNotificationsIntervalDataKey, key => { var msgs = new MessageBody[MessagePool.Count]; MessagePool.CopyTo(msgs, 0); MessagePool.Clear(); return new List(msgs); }); } } /// /// /// public class MessageBody { /// /// /// public string Message { get; set; } /// /// /// public string Category { get; set; } /// /// /// /// public override string ToString() { return string.Format("{0}-{1}", Category, Message); } } }