From 40ad69227251eabf3becf1d59c2e75fa14af166b Mon Sep 17 00:00:00 2001 From: Argo-Lenovo Date: Thu, 6 Apr 2017 18:25:51 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9BUG=EF=BC=9A=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E7=BA=BF=E7=A8=8B=E5=AE=89=E5=85=A8=E7=9A=84=E5=A0=86?= =?UTF-8?q?=E6=A0=88=E9=98=9F=E5=88=97=EF=BC=8C=E6=8E=92=E9=98=9F=E6=B6=88?= =?UTF-8?q?=E6=81=AF=EF=BC=8C=E9=98=B2=E6=AD=A2=E6=BC=8F=E6=8E=89=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E4=B8=8D=E6=8E=A8=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Bootstrap.Admin/App_Start/DBPublisher.cs | 2 +- Bootstrap.Admin/Controllers/WSController.cs | 13 ++++++----- Bootstrap.DataAccess/NotificationHelper.cs | 24 +++++++-------------- Bootstrap.DataAccess/UserHelper.cs | 2 +- 4 files changed, 18 insertions(+), 23 deletions(-) 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) {