// 大名科技(天津)有限公司版权所有 电话:18020030720 QQ:515096995
//
// 此源代码遵循位于源代码树根目录中的 LICENSE 文件的许可证
using Microsoft.AspNetCore.SignalR;
namespace Admin.NET.Core.Service;
///
/// 系统消息发送服务
///
[ApiDescriptionSettings(Order = 370)]
public class SysMessageService : IDynamicApiController, ITransient
{
private readonly SysCacheService _sysCacheService;
private readonly EmailOptions _emailOptions;
private readonly IHubContext _chatHubContext;
public SysMessageService(SysCacheService sysCacheService,
IOptions emailOptions,
IHubContext chatHubContext)
{
_sysCacheService = sysCacheService;
_emailOptions = emailOptions.Value;
_chatHubContext = chatHubContext;
}
///
/// 发送消息给所有人
///
///
///
[DisplayName("发送消息给所有人")]
public async Task SendAllUser(MessageInput input)
{
await _chatHubContext.Clients.All.ReceiveMessage(input);
}
///
/// 发送消息给除了发送人的其他人
///
///
///
[DisplayName("发送消息给除了发送人的其他人")]
public async Task SendOtherUser(MessageInput input)
{
var user = _sysCacheService.Get(CacheConst.KeyUserOnline + input.UserId);
if (user != null)
{
await _chatHubContext.Clients.AllExcept(user.ConnectionId).ReceiveMessage(input);
}
}
///
/// 发送消息给某个人
///
///
///
[DisplayName("发送消息给某个人")]
public async Task SendUser(MessageInput input)
{
var user = _sysCacheService.Get(CacheConst.KeyUserOnline + input.UserId);
if (user == null) return;
await _chatHubContext.Clients.Client(user.ConnectionId).ReceiveMessage(input);
// 可以直接通过用户Id发消息
//await _chatHubContext.Clients.User(user.UserId.ToString()).ReceiveMessage(input);
}
///
/// 发送消息给某些人
///
///
///
[DisplayName("发送消息给某些人")]
public async Task SendUsers(MessageInput input)
{
var userlist = new List();
foreach (var userid in input.UserIds)
{
var user = _sysCacheService.Get(CacheConst.KeyUserOnline + userid);
if (user != null) userlist.Add(user.ConnectionId);
}
await _chatHubContext.Clients.Clients(userlist).ReceiveMessage(input);
}
}