diff --git a/Bootstrap.Admin/App_Start/DBPublisher.cs b/Bootstrap.Admin/App_Start/DBPublisher.cs
index d49b2bfe..d8223da0 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.MessagePool.Enqueue(new MessageBody() { Category = "Notification", Message = ex.Message });
+ NotificationHelper.MessagePool.Add(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 394bc218..e3a5fc17 100644
--- a/Bootstrap.Admin/Controllers/WSController.cs
+++ b/Bootstrap.Admin/Controllers/WSController.cs
@@ -1,13 +1,7 @@
using Bootstrap.DataAccess;
-using System;
-using System.Net.Http;
-using System.Net.WebSockets;
-using System.Text;
-using System.Threading;
-using System.Threading.Tasks;
-using System.Web;
+using System.Collections.Generic;
+using System.Linq;
using System.Web.Http;
-using System.Web.WebSockets;
namespace Bootstrap.Admin.Controllers
{
@@ -19,43 +13,9 @@ namespace Bootstrap.Admin.Controllers
///
///
[HttpGet]
- public HttpResponseMessage Get()
+ public List Get()
{
- var response = new HttpResponseMessage();
- if (HttpContext.Current.IsWebSocketRequest)
- {
- HttpContext.Current.AcceptWebSocketRequest(MessageHandler);
- response.StatusCode = System.Net.HttpStatusCode.SwitchingProtocols;
- }
- else
- {
- response.Content = new StringContent("请使用WebSocket协议请求");
- }
- return response;
- }
- ///
- ///
- ///
- ///
- ///
- private async System.Threading.Tasks.Task MessageHandler(AspNetWebSocketContext arg)
- {
- WebSocket socket = arg.WebSocket;
- while (socket.State == WebSocketState.Open)
- {
- if (NotificationHelper.MessagePool.IsEmpty)
- {
- await System.Threading.Tasks.Task.Delay(300);
- continue;
- }
- 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);
- }
- }
+ return NotificationHelper.MessagePool.ToList();
}
}
}
\ No newline at end of file
diff --git a/Bootstrap.Admin/Scripts/common-scripts.js b/Bootstrap.Admin/Scripts/common-scripts.js
index 73dc4c1c..ed35dca8 100644
--- a/Bootstrap.Admin/Scripts/common-scripts.js
+++ b/Bootstrap.Admin/Scripts/common-scripts.js
@@ -34,7 +34,7 @@
}
});
},
- resetWidget() {
+ resetWidget: function () {
var widgets = $(this).children('li');
widgets.each(function () {
var widget = $(this).children('ul');
@@ -49,7 +49,37 @@
});
$.extend({
- reloadWidget() {
+ pullNotification: function () {
+ if ($('.notify-row').length == 0) return;
+ setTimeout(function () {
+ NProgress.status = true;
+ NProgress.configure({ trickle: false });
+ $.bc({
+ url: '../api/WS',
+ method: 'GET',
+ swal: false,
+ callback: function (result) {
+ NProgress.status = false;
+ for (index in result) {
+ var cate = result[index].Category;
+ var msg = result[index].Message;
+ switch (cate) {
+ case "Notification":
+ toastr.error(msg, "应用程序出现错误");
+ break;
+ case "Users":
+ toastr.info(msg, "新用户注册");
+ break;
+ }
+ };
+ if (result.length > 0) $.reloadWidget();
+ $.pullNotification();
+ }
+ });
+ }, 5000);
+ },
+ reloadWidget: function () {
+ if ($('.notify-row').length == 0) return;
$.bc({
url: Notifications.url,
swal: false,
@@ -178,27 +208,5 @@ $(function () {
// load widget data
$.reloadWidget();
-
- var ws = new WebSocket($.format("ws://{0}/api/WS", window.location.host));
- ws.onerror = function (error) {
- console.log(error);
- };
- ws.onmessage = function (msg) {
- console.log(msg.data);
- var data = msg.data.split(';');
- if (data.length !== 2) return;
- switch (data[0]) {
- case "Notification":
- toastr.error(data[1], "应用程序出现错误");
- $.reloadWidget();
- break;
- case "Users":
- toastr.info(data[1], "新用户注册");
- $.reloadWidget();
- break;
- }
- };
- ws.onclose = function () {
- console.log("Disconnected!");
- };
+ $.pullNotification();
});
\ No newline at end of file
diff --git a/Bootstrap.DataAccess/NotificationHelper.cs b/Bootstrap.DataAccess/NotificationHelper.cs
index 9fcd2cf0..aa926862 100644
--- a/Bootstrap.DataAccess/NotificationHelper.cs
+++ b/Bootstrap.DataAccess/NotificationHelper.cs
@@ -7,6 +7,8 @@ using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
+using System.Net.WebSockets;
+using System.Threading;
namespace Bootstrap.DataAccess
{
@@ -22,7 +24,7 @@ namespace Bootstrap.DataAccess
///
///
///
- public static readonly ConcurrentQueue MessagePool = new ConcurrentQueue();
+ public static readonly ConcurrentBag MessagePool = new ConcurrentBag();
///
/// 新用户注册的通知的面板显示
///
@@ -124,8 +126,20 @@ namespace Bootstrap.DataAccess
///
///
///
- public class MessageBody
+ public class MessageBody : IDisposable
{
+ ///
+ ///
+ ///
+ public MessageBody()
+ {
+ timer = new Timer(state =>
+ {
+ var msg = this;
+ NotificationHelper.MessagePool.TryTake(out msg);
+ }, null, 5000, Timeout.Infinite);
+ }
+ private Timer timer = null;
///
///
///
@@ -140,7 +154,26 @@ namespace Bootstrap.DataAccess
///
public override string ToString()
{
- return string.Format("{0};{1}", Category, Message);
+ return string.Format("{0};{1}-{2}", Category, Message);
+ }
+ private void Dispose(bool disposing)
+ {
+ if (disposing)
+ {
+ if (timer != null)
+ {
+ timer.Dispose();
+ timer = null;
+ }
+ }
+ }
+ ///
+ ///
+ ///
+ public void Dispose()
+ {
+ Dispose(true);
+ GC.SuppressFinalize(this);
}
}
}
diff --git a/Bootstrap.DataAccess/UserHelper.cs b/Bootstrap.DataAccess/UserHelper.cs
index b74b6915..403d5504 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.MessagePool.Enqueue(new MessageBody() { Category = "Users", Message = string.Format("{0}-{1}", p.UserName, p.Description) });
+ if (p.UserStatus == 1) NotificationHelper.MessagePool.Add(new MessageBody() { Category = "Users", Message = string.Format("{0}-{1}", p.UserName, p.Description) });
}
catch (DbException ex)
{