refactor: SignalRManager 更改为 Extensions

comment 重构扩展方法,为消息分组做准备
This commit is contained in:
Argo-Mac 2019-07-30 11:13:01 +08:00
parent 91c4ce98bd
commit 4371e3c282
No known key found for this signature in database
GPG Key ID: 152E398953DDF19F
7 changed files with 79 additions and 60 deletions

View File

@ -37,7 +37,7 @@ namespace Bootstrap.Admin.Controllers.Api
public async Task<bool> Post([FromServices]IHubContext<SignalRHub> hub, [FromBody]User user)
{
var ret = UserHelper.Save(user);
if (ret) await SignalRManager.Send(hub.Clients.All, new MessageBody() { Category = "Users", Message = string.Format("{0}-{1}", user.UserName, user.Description) });
if (ret) await hub.SendMessageBody(new MessageBody() { Category = "Users", Message = string.Format("{0}-{1}", user.UserName, user.Description) }, HttpContext.RequestAborted);
return ret;
}

View File

@ -1,8 +1,8 @@
using Longbow.Tasks;
using Longbow.Web.SignalR;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using System.Linq;
using System.Threading.Tasks;
namespace Bootstrap.Admin.Controllers.Api
{
@ -20,19 +20,19 @@ namespace Bootstrap.Admin.Controllers.Api
/// <param name="hub"></param>
/// <returns></returns>
[HttpGet]
public bool Get([FromQuery]string name, [FromServices]IHubContext<SignalRHub> hub)
public async Task<ActionResult> Get([FromQuery]string name, [FromServices]IHubContext<TaskLogHub> hub)
{
var sche = TaskServicesManager.GetOrAdd(name);
sche.Triggers.First().PulseCallback = t => SendTaskLog(sche, name, hub);
SendTaskLog(sche, name, hub);
return true;
sche.Triggers.First().PulseCallback = t => SendTaskLog(sche, name, hub).ConfigureAwait(false);
await SendTaskLog(sche, name, hub).ConfigureAwait(false);
return Ok(true);
}
private void SendTaskLog(IScheduler sche, string name, IHubContext<SignalRHub> hub)
private async Task SendTaskLog(IScheduler sche, string name, IHubContext<TaskLogHub> hub)
{
var t = sche.Triggers.First();
var result = $"{{\"name\": \"{name}\", \"msg\": \"Trigger({t.GetType().Name}) LastRuntime: {sche.LastRuntime} Run({t.LastResult}) NextRuntime: {sche.NextRuntime} Elapsed: {t.LastRunElapsedTime.Seconds}s\"}}";
SignalRManager.SendTaskLog(hub.Clients.All, result).ConfigureAwait(false);
await hub.SendTaskLog(result);
}
}
}

View File

@ -1,43 +0,0 @@
using Bootstrap.DataAccess;
using Microsoft.AspNetCore.SignalR;
using System;
using System.Data.Common;
namespace Bootstrap.Admin
{
/// <summary>
///
/// </summary>
public static class SignalRManager
{
/// <summary>
///
/// </summary>
/// <param name="client"></param>
/// <param name="args"></param>
/// <returns></returns>
public static System.Threading.Tasks.Task Send(IClientProxy client, MessageBody args) => client.SendAsync("rev", args);
/// <summary>
///
/// </summary>
/// <param name="client"></param>
/// <param name="args"></param>
/// <returns></returns>
public static System.Threading.Tasks.Task SendTaskLog(IClientProxy client, string args) => client.SendAsync("taskRev", args);
/// <summary>
///
/// </summary>
/// <param name="context"></param>
/// <param name="ex"></param>
/// <returns></returns>
public static async System.Threading.Tasks.Task Send<T>(IHubContext<T> context, Exception ex) where T : Hub
{
var category = "App";
if (ex.GetType().IsSubclassOf(typeof(DbException))) category = "DB";
var message = new MessageBody() { Category = category, Message = ex.Message };
await Send(context.Clients.All, message);
}
}
}

View File

@ -0,0 +1,47 @@
using Bootstrap.DataAccess;
using Longbow.Web.SignalR;
using Microsoft.AspNetCore.SignalR;
using System;
using System.Data.Common;
using System.Threading;
using Task = System.Threading.Tasks.Task;
namespace Bootstrap.Admin
{
/// <summary>
/// SignalR 操作扩展类
/// </summary>
public static class SignalRExtensions
{
/// <summary>
/// 推送异常信息到客户端方法扩展
/// </summary>
/// <param name="context"></param>
/// <param name="ex"></param>
/// <param name="token"></param>
/// <returns></returns>
public static async Task SendMessageBody(this IHubContext<SignalRHub> context, Exception ex, CancellationToken token = default)
{
var category = "App";
if (ex.GetType().IsSubclassOf(typeof(DbException))) category = "DB";
await context.SendMessageBody(new MessageBody() { Category = category, Message = ex.Message }, token);
}
/// <summary>
/// 推送 MessageBody 到客户端方法扩展
/// </summary>
/// <param name="context"></param>
/// <param name="messageBody"></param>
/// <param name="token"></param>
/// <returns></returns>
public static Task SendMessageBody(this IHubContext<SignalRHub> context, MessageBody messageBody, CancellationToken token = default) => context.Clients.All.SendAsync("rev", messageBody, token);
/// <summary>
/// 推送任务消息到客户端扩展
/// </summary>
/// <param name="context"></param>
/// <param name="args"></param>
/// <returns></returns>
public static Task SendTaskLog(this IHubContext<TaskLogHub> context, string args) => context.Clients.All.SendAsync("rev", args);
}
}

View File

@ -18,12 +18,12 @@ using System.Text.Unicode;
namespace Bootstrap.Admin
{
/// <summary>
///
/// Startup 启动配置文件
/// </summary>
public class Startup
{
/// <summary>
///
/// 构造函数
/// </summary>
/// <param name="configuration"></param>
public Startup(IConfiguration configuration)
@ -32,13 +32,13 @@ namespace Bootstrap.Admin
}
/// <summary>
///
/// 获得 系统配置项 Iconfiguration 实例
/// </summary>
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
/// <summary>
///
/// 服务容器注入方法
/// </summary>
/// <param name="services"></param>
public void ConfigureServices(IServiceCollection services)
@ -58,7 +58,7 @@ namespace Bootstrap.Admin
services.AddIPLocator(DictHelper.ConfigIPLocator);
services.AddOnlineUsers();
services.AddSignalR().AddJsonProtocalDefault();
services.AddSignalRExceptionFilterHandler<SignalRHub>(async (client, ex) => await SignalRManager.Send(client, ex));
services.AddSignalRExceptionFilterHandler<SignalRHub>((client, ex) => client.SendMessageBody(ex).ConfigureAwait(false));
services.AddResponseCompression();
services.AddBootstrapAdminAuthentication();
services.AddSwagger();
@ -86,7 +86,7 @@ namespace Bootstrap.Admin
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
/// <summary>
///
/// 管道构建方法
/// </summary>
/// <param name="app"></param>
/// <param name="env"></param>
@ -110,7 +110,11 @@ namespace Bootstrap.Admin
app.UseBootstrapAdminAuthentication(RoleHelper.RetrievesByUserName, RoleHelper.RetrievesByUrl, AppHelper.RetrievesByUserName);
app.UseOnlineUsers(callback: TraceHelper.Save);
app.UseCacheManagerCorsHandler();
app.UseSignalR(routes => { routes.MapHub<SignalRHub>("/NotiHub"); });
app.UseSignalR(routes =>
{
routes.MapHub<SignalRHub>("/NotiHub");
routes.MapHub<TaskLogHub>("/TaskLogHub");
});
app.UseSwagger(Configuration["SwaggerPathBase"].TrimEnd('/'));
app.UseMvc(routes =>
{

View File

@ -0,0 +1,12 @@
using Longbow.Web.SignalR;
namespace Bootstrap.Admin
{
/// <summary>
/// 后台任务消息Hub
/// </summary>
public class TaskLogHub : SignalRHub
{
}
}

View File

@ -67,8 +67,7 @@
// open hub
$taskMsg.notifi({
url: 'NotiHub',
method: 'taskRev',
url: 'TaskLogHub',
callback: function (result) {
var content = this.children();
while (content.children().length > 50) {