1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- 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;
- }
- /// <summary>
- /// Called when a new connection is established with the hub.
- /// </summary>
- /// <returns>A <see cref="T:System.Threading.Tasks.Task" /> that represents the asynchronous connect.</returns>
- 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();
- }
- /// <summary>Called when a connection with the hub is terminated.</summary>
- /// <returns>A <see cref="T:System.Threading.Tasks.Task" /> that represents the asynchronous disconnect.</returns>
- public override Task OnDisconnectedAsync(Exception? exception)
- {
- return base.OnDisconnectedAsync(exception);
- }
- }
|