添加项目文件。
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Common;
|
||||
using Common.Utils;
|
||||
using CommonDomain;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using RedisService;
|
||||
using StackExchange.Redis;
|
||||
|
||||
namespace CommonService
|
||||
{
|
||||
public class HubSend<T> : IHubSend<T> where T : Hub
|
||||
{
|
||||
private readonly ILogger<HubSend<T>> _logger;
|
||||
private readonly IHubContext<T> _pushHub;
|
||||
private readonly IRedisService _cache;
|
||||
|
||||
public HubSend(ILogger<HubSend<T>> logger, IHubContext<T> pushHub, IRedisService cache)
|
||||
{
|
||||
_logger = logger;
|
||||
_pushHub = pushHub;
|
||||
_cache = cache;
|
||||
}
|
||||
|
||||
public void SendUserUserId(string userId, string methon, object obj)
|
||||
{
|
||||
var userRedis = _cache.GetRedisUserCId(userId);
|
||||
if (userRedis != null)
|
||||
{
|
||||
SendUserCId(userRedis.CId, methon, obj);
|
||||
}
|
||||
}
|
||||
|
||||
public void SendUserUserIds(List<string> userIds, string methon, object obj)
|
||||
{
|
||||
foreach (var item in userIds)
|
||||
{
|
||||
var userRedis = _cache.GetRedisUserCId(item);
|
||||
if (userRedis != null)
|
||||
{
|
||||
SendUserCId(userRedis.CId, methon, obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void SendUserCId(string cid, string methon, object obj)
|
||||
{
|
||||
ObserveSend(_pushHub.Clients.Client(cid).SendAsync(methon, obj), methon);
|
||||
}
|
||||
|
||||
public Task SendUserCIdAsync(string cid, string method, object obj)
|
||||
{
|
||||
return _pushHub.Clients.Client(cid).SendAsync(method, obj);
|
||||
}
|
||||
|
||||
public void SendUserCIds(List<string> cids, string methon, object obj)
|
||||
{
|
||||
ObserveSend(_pushHub.Clients.Clients(cids).SendAsync(methon, obj), methon);
|
||||
}
|
||||
|
||||
public void SendGroup(string groupId, string method, object obj)
|
||||
{
|
||||
ObserveSend(_pushHub.Clients.Group(groupId).SendAsync(method, obj), method);
|
||||
}
|
||||
|
||||
public Task SendGroupAsync(string groupId, string method, object obj)
|
||||
{
|
||||
return _pushHub.Clients.Group(groupId).SendAsync(method, obj);
|
||||
}
|
||||
|
||||
public void RemoveGroup(string cid, string groupId)
|
||||
{
|
||||
_pushHub.Groups.RemoveFromGroupAsync(cid, groupId).GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
public void GroupExcept(string cid, string groupId, string method, object obj)
|
||||
{
|
||||
ObserveSend(_pushHub.Clients.GroupExcept(groupId, cid).SendAsync(method, obj), method);
|
||||
}
|
||||
|
||||
public Task GroupExceptAsync(string cid, string groupId, string method, object obj)
|
||||
{
|
||||
return _pushHub.Clients.GroupExcept(groupId, cid).SendAsync(method, obj);
|
||||
}
|
||||
|
||||
public void AddGroup(string cid, string groupId)
|
||||
{
|
||||
_pushHub.Groups.AddToGroupAsync(cid, groupId).GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
public void SendAll(string method, object obj)
|
||||
{
|
||||
ObserveSend(_pushHub.Clients.All.SendAsync(method, obj), method);
|
||||
}
|
||||
|
||||
private void ObserveSend(Task sendTask, string method)
|
||||
{
|
||||
if (sendTask.IsCompletedSuccessfully)
|
||||
return;
|
||||
|
||||
_ = sendTask.ContinueWith(
|
||||
task => _logger.LogWarning(task.Exception, "SignalR 消息发送失败: {Method}", method),
|
||||
CancellationToken.None,
|
||||
TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.ExecuteSynchronously,
|
||||
TaskScheduler.Default);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user