增加功能:Administrators组成员,能够实时接收后台消息推送

This commit is contained in:
Argo-Lenovo 2017-04-09 14:30:10 +08:00
parent 141c14a6f2
commit 21a144d697
5 changed files with 75 additions and 74 deletions

View File

@ -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 });
}
}
}

View File

@ -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
/// <param name="value"></param>
/// <returns></returns>
[HttpGet]
public HttpResponseMessage Get()
public List<MessageBody> 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;
}
/// <summary>
///
/// </summary>
/// <param name="arg"></param>
/// <returns></returns>
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<byte> buffer = new ArraySegment<byte>(new byte[1024]);
buffer = new ArraySegment<byte>(Encoding.UTF8.GetBytes(msg.ToString()));
await socket.SendAsync(buffer, WebSocketMessageType.Text, true, CancellationToken.None);
}
}
return NotificationHelper.MessagePool.ToList();
}
}
}

View File

@ -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();
});

View File

@ -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
/// <summary>
///
/// </summary>
public static readonly ConcurrentQueue<MessageBody> MessagePool = new ConcurrentQueue<MessageBody>();
public static readonly ConcurrentBag<MessageBody> MessagePool = new ConcurrentBag<MessageBody>();
/// <summary>
/// 新用户注册的通知的面板显示
/// </summary>
@ -124,8 +126,20 @@ namespace Bootstrap.DataAccess
/// <summary>
///
/// </summary>
public class MessageBody
public class MessageBody : IDisposable
{
/// <summary>
///
/// </summary>
public MessageBody()
{
timer = new Timer(state =>
{
var msg = this;
NotificationHelper.MessagePool.TryTake(out msg);
}, null, 5000, Timeout.Infinite);
}
private Timer timer = null;
/// <summary>
///
/// </summary>
@ -140,7 +154,26 @@ namespace Bootstrap.DataAccess
/// <returns></returns>
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;
}
}
}
/// <summary>
///
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
}

View File

@ -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)
{