using Hotline.Caches; using Hotline.Users; using Microsoft.AspNetCore.SignalR; using XF.Domain.Authentications; using XF.Domain.Exceptions; namespace Hotline.Api.Realtimes; public class CallCenterHub : Hub { private readonly ISessionContext _sessionContext; private readonly IWorkRepository _workRepository; private readonly IUserCacheManager _userCacheManager; public CallCenterHub( ISessionContext sessionContext, IWorkRepository workRepository, IUserCacheManager userCacheManager) { _sessionContext = sessionContext; _workRepository = workRepository; _userCacheManager = userCacheManager; } /// /// Called when a new connection is established with the hub. /// /// A that represents the asynchronous connect. public override async Task OnConnectedAsync() { var userId = _sessionContext.RequiredUserId; var work = _userCacheManager.GetWorkByUser(userId); if (work == null) throw new UserFriendlyException($"未查询到上班记录, userId: {userId}"); work.SignalRId = Context.ConnectionId; await _workRepository.UpdateAsync(work, Context.ConnectionAborted); _userCacheManager.UpdateWorkByUser(work); await base.OnConnectedAsync(); } /// Called when a connection with the hub is terminated. /// A that represents the asynchronous disconnect. public override Task OnDisconnectedAsync(Exception? exception) { return base.OnDisconnectedAsync(exception); } }