diff --git a/Bootstrap.Admin/App_Start/DBPublisher.cs b/Bootstrap.Admin/App_Start/DBPublisher.cs index 4d679091..d49b2bfe 100644 --- a/Bootstrap.Admin/App_Start/DBPublisher.cs +++ b/Bootstrap.Admin/App_Start/DBPublisher.cs @@ -23,7 +23,7 @@ namespace Bootstrap.Admin if (publisherElement.Mode == PublisherMode.Off) return; ExceptionHelper.Log(ex, additionalInfo); CacheManager.Clear(k => k == ExceptionHelper.RetrieveExceptionsDataKey); - NotificationHelper.PushMessage(new MessageBody() { Category = "Notification", Message = ex.Message }); + NotificationHelper.MessagePool.Enqueue(new MessageBody() { Category = "Notification", Message = ex.Message }); } } } \ No newline at end of file diff --git a/Bootstrap.Admin/Controllers/WSController.cs b/Bootstrap.Admin/Controllers/WSController.cs index 391b4f58..394bc218 100644 --- a/Bootstrap.Admin/Controllers/WSController.cs +++ b/Bootstrap.Admin/Controllers/WSController.cs @@ -43,15 +43,18 @@ namespace Bootstrap.Admin.Controllers WebSocket socket = arg.WebSocket; while (socket.State == WebSocketState.Open) { - if (!NotificationHelper.Push) + if (NotificationHelper.MessagePool.IsEmpty) { await System.Threading.Tasks.Task.Delay(300); continue; } - ArraySegment buffer = new ArraySegment(new byte[1024]); - buffer = new ArraySegment(Encoding.UTF8.GetBytes(NotificationHelper.Message.ToString())); - await socket.SendAsync(buffer, WebSocketMessageType.Text, true, CancellationToken.None); - NotificationHelper.Push = false; + var msg = new MessageBody(); + if (NotificationHelper.MessagePool.TryDequeue(out msg)) + { + ArraySegment buffer = new ArraySegment(new byte[1024]); + buffer = new ArraySegment(Encoding.UTF8.GetBytes(msg.ToString())); + await socket.SendAsync(buffer, WebSocketMessageType.Text, true, CancellationToken.None); + } } } } diff --git a/Bootstrap.DataAccess/NotificationHelper.cs b/Bootstrap.DataAccess/NotificationHelper.cs index e14a9017..9fcd2cf0 100644 --- a/Bootstrap.DataAccess/NotificationHelper.cs +++ b/Bootstrap.DataAccess/NotificationHelper.cs @@ -2,6 +2,7 @@ using Longbow.Caching; using Longbow.ExceptionManagement; using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Data; using System.Data.Common; @@ -14,8 +15,15 @@ namespace Bootstrap.DataAccess /// public static class NotificationHelper { + /// + /// + /// internal const string RetrieveNotificationsDataKey = "NotificationHelper-RetrieveNotifications"; /// + /// + /// + public static readonly ConcurrentQueue MessagePool = new ConcurrentQueue(); + /// /// 新用户注册的通知的面板显示 /// /// @@ -112,22 +120,6 @@ namespace Bootstrap.DataAccess } return ret; } - /// - /// 推送的消息 - /// - public static MessageBody Message { get; private set; } - /// - /// 获得/设置 是否推送消息 - /// - public static bool Push { get; set; } - /// - /// - /// - public static void PushMessage(MessageBody message) - { - Push = true; - Message = message; - } } /// /// diff --git a/Bootstrap.DataAccess/UserHelper.cs b/Bootstrap.DataAccess/UserHelper.cs index 42a6fc19..b74b6915 100644 --- a/Bootstrap.DataAccess/UserHelper.cs +++ b/Bootstrap.DataAccess/UserHelper.cs @@ -145,7 +145,7 @@ namespace Bootstrap.DataAccess } CacheCleanUtility.ClearCache(userIds: p.Id == 0 ? string.Empty : p.Id.ToString()); ret = true; - if (p.UserStatus == 1) NotificationHelper.PushMessage(new MessageBody() { Category = "Users", Message = string.Format("{0}-{1}", p.UserName, p.Description) }); + if (p.UserStatus == 1) NotificationHelper.MessagePool.Enqueue(new MessageBody() { Category = "Users", Message = string.Format("{0}-{1}", p.UserName, p.Description) }); } catch (DbException ex) {