| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- using Admin.NET.Core.Service;
- using Microsoft.AspNetCore.SignalR;
- namespace Admin.NET.Plugin.ApprovalFlow.Service;
- /// <summary>
- /// SignalR 站内实时推送(P4-16 - 已有实现重构为独立 Pusher)
- /// </summary>
- public class SignalRNotifyPusher : INotifyPusher, ITransient
- {
- public string Channel => "SignalR";
- private readonly IHubContext<OnlineUserHub, IOnlineUserHub> _hubContext;
- private readonly SysCacheService _cacheService;
- public SignalRNotifyPusher(
- IHubContext<OnlineUserHub, IOnlineUserHub> hubContext,
- SysCacheService cacheService)
- {
- _hubContext = hubContext;
- _cacheService = cacheService;
- }
- public bool IsEnabled(NotifyChannelConfig cfg) => cfg?.SignalR != false;
- public async Task<FlowNotifyPushResult> PushAsync(List<long> userIds, FlowNotification notification)
- {
- try
- {
- var onlineUsers = _cacheService.HashGetAll<SysOnlineUser>(CacheConst.KeyUserOnline);
- var connectionIds = onlineUsers
- .Where(u => userIds.Contains(u.Value.UserId))
- .Select(u => u.Value.ConnectionId)
- .ToList();
- if (connectionIds.Count == 0)
- return FlowNotifyPushResult.Ok(0);
- await _hubContext.Clients.Clients(connectionIds).ReceiveMessage(new
- {
- title = notification.Title,
- message = notification.Content,
- type = notification.Type.ToString(),
- instanceId = notification.InstanceId,
- });
- return FlowNotifyPushResult.Ok(connectionIds.Count);
- }
- catch (Exception ex)
- {
- return FlowNotifyPushResult.Fail(ex.Message);
- }
- }
- }
|