2018-09-07 15:53:16 +08:00
|
|
|
|
using Longbow.Cache;
|
|
|
|
|
using Longbow.Web.WebSockets;
|
|
|
|
|
using Newtonsoft.Json;
|
2016-11-11 13:11:57 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2018-09-07 15:53:16 +08:00
|
|
|
|
using System.Text;
|
2016-11-11 13:11:57 +08:00
|
|
|
|
|
|
|
|
|
namespace Bootstrap.DataAccess
|
|
|
|
|
{
|
2017-03-30 16:15:45 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static class NotificationHelper
|
2016-11-11 13:11:57 +08:00
|
|
|
|
{
|
2017-04-06 18:25:51 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
2018-09-07 15:53:16 +08:00
|
|
|
|
private const string PullNotificationsIntervalDataKey = "NotificationHelper-PullNotificationsInterval";
|
|
|
|
|
private static readonly List<MessageBody> MessagePool = new List<MessageBody>();
|
2016-11-11 13:11:57 +08:00
|
|
|
|
/// <summary>
|
2017-04-06 18:25:51 +08:00
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
2018-09-07 15:53:16 +08:00
|
|
|
|
/// <param name="message"></param>
|
2016-11-11 13:11:57 +08:00
|
|
|
|
/// <returns></returns>
|
2018-09-07 15:53:16 +08:00
|
|
|
|
public static void PushMessage(MessageBody message)
|
2016-11-11 13:11:57 +08:00
|
|
|
|
{
|
2018-09-07 15:53:16 +08:00
|
|
|
|
MessagePool.Add(message);
|
|
|
|
|
CacheManager.Clear(PullNotificationsIntervalDataKey);
|
2016-11-11 13:11:57 +08:00
|
|
|
|
|
2018-09-07 15:53:16 +08:00
|
|
|
|
// websocket message push
|
|
|
|
|
WebSocketServerManager.SendAsync(new ArraySegment<byte>(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new MessageBody[] { message }))));
|
2016-11-11 13:11:57 +08:00
|
|
|
|
}
|
2016-11-18 10:18:41 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2018-09-07 15:53:16 +08:00
|
|
|
|
public static IEnumerable<MessageBody> RetrieveMessages()
|
2016-11-18 10:18:41 +08:00
|
|
|
|
{
|
2018-09-07 15:53:16 +08:00
|
|
|
|
return CacheManager.GetOrAdd(PullNotificationsIntervalDataKey, key =>
|
2016-11-18 10:18:41 +08:00
|
|
|
|
{
|
2018-09-07 15:53:16 +08:00
|
|
|
|
var msgs = new MessageBody[MessagePool.Count];
|
|
|
|
|
MessagePool.CopyTo(msgs, 0);
|
|
|
|
|
MessagePool.Clear();
|
|
|
|
|
return new List<MessageBody>(msgs);
|
|
|
|
|
});
|
2016-11-18 10:18:41 +08:00
|
|
|
|
}
|
2017-04-06 17:55:24 +08:00
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
2018-09-07 15:53:16 +08:00
|
|
|
|
public class MessageBody
|
2017-04-06 17:55:24 +08:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string Message { get; set; }
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string Category { get; set; }
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
2018-06-11 12:48:11 +08:00
|
|
|
|
return string.Format("{0}-{1}", Category, Message);
|
2017-04-09 14:30:10 +08:00
|
|
|
|
}
|
2016-11-11 13:11:57 +08:00
|
|
|
|
}
|
|
|
|
|
}
|