Blog.Core/Blog.Core.Extensions/Middlewares/SignalRSendMiddleware.cs
2023-04-01 23:25:56 +08:00

48 lines
1.2 KiB
C#

using System.Threading.Tasks;
using Blog.Core.Common;
using Blog.Core.Common.LogHelper;
using Blog.Core.Hubs;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.SignalR;
namespace Blog.Core.Extensions.Middlewares
{
/// <summary>
/// 中间件
/// SignalR发送数据
/// </summary>
public class SignalRSendMiddleware
{
/// <summary>
///
/// </summary>
private readonly RequestDelegate _next;
private readonly IHubContext<ChatHub> _hubContext;
/// <summary>
///
/// </summary>
/// <param name="next"></param>
/// <param name="hubContext"></param>
public SignalRSendMiddleware(RequestDelegate next, IHubContext<ChatHub> hubContext)
{
_next = next;
_hubContext = hubContext;
}
public async Task InvokeAsync(HttpContext context)
{
if (AppSettings.app("Middleware", "SignalR", "Enabled").ObjToBool())
{
//TODO 主动发送错误消息
await _hubContext.Clients.All.SendAsync("ReceiveUpdate", LogLock.GetLogData());
}
await _next(context);
}
}
}