using Cksoft.Unity; using Cksoft.Unity.Log4NetConfig; using DllEapBll.Services; using DllEapCommon.Filters; using DllEapEntity.OFILM; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.SignalR; using Microsoft.Extensions.Configuration; using System; using System.Collections.Generic; using System.Configuration.Internal; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace DllEapBll.SignalR { public class MacStatusHub : Hub { private readonly MacStatusService macStatusService; private readonly IConfiguration configuration; private readonly int seconds = 60; public MacStatusHub(MacStatusService macStatusService, IConfiguration configuration) { this.configuration = configuration; this.macStatusService = macStatusService; } [AllowAnonymous] [ExceptionFilter] public async Task GetLastStatus(string filter, string recipe, string date, string type) { EapResponse response = null; var connectionId = Context.ConnectionId; try { while (true) { //if () //{ // break; //} response = macStatusService.GetTotalInfo(filter, recipe, date, type); await Clients.Client(connectionId).SendAsync("ReceiveUpdate", response); Thread.Sleep(1000 * seconds); } } catch (Exception e) { await Clients.Client(connectionId).SendAsync("Error", e.Message); } finally { await Clients.Client(connectionId).SendAsync("Finished"); Context.Abort(); Dispose(); } } [ExceptionFilter] public override async Task OnConnectedAsync() { var connectionId = Context.ConnectionId; var context = Context.GetHttpContext(); var filter = Context.GetHttpContext().Request.Query["filter"].FirstOrDefault(); var recipe = context.Request.Query["recipe"].FirstOrDefault(); var date = context.Request.Query["date"].FirstOrDefault(); var type = context.Request.Query["type"].FirstOrDefault(); await Clients.Client(connectionId).SendAsync("BeginConnection", macStatusService.GetTotalInfo(filter, recipe, date, type)); Thread.Sleep(1000 * seconds); await GetLastStatus(filter, recipe, date, type); } public override async Task OnDisconnectedAsync(Exception exception) { LogHelper.LogError(exception?.ToString() ?? "SignalR断开连接", "System", ""); await Task.CompletedTask; } } }