feat: 完善聊天模块
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
using FreeRedis;
|
||||
using Mapster;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Volo.Abp.Caching;
|
||||
using Volo.Abp.Domain.Services;
|
||||
using Yi.Framework.ChatHub.Domain.Shared.Caches;
|
||||
using Yi.Framework.ChatHub.Domain.Shared.Consts;
|
||||
using Yi.Framework.ChatHub.Domain.Shared.Model;
|
||||
using Yi.Framework.ChatHub.Domain.SignalRHubs;
|
||||
|
||||
namespace Yi.Framework.ChatHub.Domain.Managers
|
||||
{
|
||||
public class UserMessageManager : DomainService
|
||||
{
|
||||
private IHubContext<ChatCenterHub> _hubContext;
|
||||
public UserMessageManager(IHubContext<ChatCenterHub> hubContext)
|
||||
{
|
||||
_hubContext = hubContext;
|
||||
}
|
||||
/// <summary>
|
||||
/// 使用懒加载防止报错
|
||||
/// </summary>
|
||||
private IRedisClient RedisClient => LazyServiceProvider.LazyGetRequiredService<IRedisClient>();
|
||||
private string CacheKeyPrefix => LazyServiceProvider.LazyGetRequiredService<IOptions<AbpDistributedCacheOptions>>().Value.KeyPrefix;
|
||||
|
||||
public async Task SendMessageAsync(MessageContext context)
|
||||
{
|
||||
switch (context.MessageType)
|
||||
{
|
||||
case MessageType.Personal:
|
||||
var userModel =await GetUserAsync(context.ReceiveId.Value);
|
||||
if (userModel is not null)
|
||||
{
|
||||
await _hubContext.Clients.Client(userModel.ClientId).SendAsync(ChatConst.ClientActionReceiveMsg, context.MessageType, context);
|
||||
}
|
||||
break;
|
||||
case MessageType.Group:
|
||||
throw new NotImplementedException();
|
||||
break;
|
||||
case MessageType.All:
|
||||
await _hubContext.Clients.All.SendAsync(ChatConst.ClientActionReceiveMsg, context.MessageType, context);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public async Task<List<ChatOnlineUserCacheItem>> GetAllUserAsync()
|
||||
{
|
||||
var key = new ChatOnlineUserCacheKey(CacheKeyPrefix);
|
||||
var cacheUsers = (await RedisClient.HGetAllAsync(key.GetKey())).Select(x => System.Text.Json.JsonSerializer.Deserialize<ChatOnlineUserCacheItem>(x.Value)).ToList();
|
||||
return cacheUsers;
|
||||
}
|
||||
|
||||
public async Task<ChatOnlineUserCacheItem?> GetUserAsync(Guid userId)
|
||||
{
|
||||
var key = new ChatOnlineUserCacheKey(CacheKeyPrefix);
|
||||
var cacheUser = System.Text.Json.JsonSerializer.Deserialize < ChatOnlineUserCacheItem >( await RedisClient.HGetAsync(key.GetKey(), key.GetField(userId)));
|
||||
return cacheUser;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
using FreeRedis;
|
||||
using Mapster;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Volo.Abp.AspNetCore.SignalR;
|
||||
using Volo.Abp.Caching;
|
||||
using Yi.Framework.ChatHub.Domain.Shared.Caches;
|
||||
using Yi.Framework.ChatHub.Domain.Shared.Consts;
|
||||
using Yi.Framework.ChatHub.Domain.Shared.Model;
|
||||
|
||||
namespace Yi.Framework.ChatHub.Domain.SignalRHubs
|
||||
{
|
||||
[HubRoute("/hub/chat")]
|
||||
[Authorize]
|
||||
public class ChatCenterHub : AbpHub
|
||||
{
|
||||
/// <summary>
|
||||
/// 使用懒加载防止报错
|
||||
/// </summary>
|
||||
private IRedisClient RedisClient => LazyServiceProvider.LazyGetRequiredService<IRedisClient>();
|
||||
/// <summary>
|
||||
/// 缓存前缀
|
||||
/// </summary>
|
||||
private string CacheKeyPrefix => LazyServiceProvider.LazyGetRequiredService<IOptions<AbpDistributedCacheOptions>>().Value.KeyPrefix;
|
||||
public ChatCenterHub()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用户进入聊天室
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public override async Task OnConnectedAsync()
|
||||
{
|
||||
var key = new ChatOnlineUserCacheKey(CacheKeyPrefix);
|
||||
var item = new ChatOnlineUserCacheItem()
|
||||
{
|
||||
UserId = CurrentUser.Id!.Value,
|
||||
ClientId = Context.ConnectionId,
|
||||
UserName = CurrentUser.UserName!
|
||||
};
|
||||
|
||||
|
||||
await RedisClient.HSetAsync(key.GetKey(), key.GetField(CurrentUser.Id!.Value), item);
|
||||
|
||||
|
||||
//连接时,还需要去查询用户包含在的群组,将群主全部加入.Todo
|
||||
await Groups.AddToGroupAsync(Context.ConnectionId, ChatConst.AllGroupName);
|
||||
await Clients.All.SendAsync("liveUser", item.Adapt<ChatUserModel>());
|
||||
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用户退出聊天室
|
||||
/// </summary>
|
||||
/// <param name="exception"></param>
|
||||
/// <returns></returns>
|
||||
public async override Task OnDisconnectedAsync(Exception? exception)
|
||||
{
|
||||
var key = new ChatOnlineUserCacheKey(CacheKeyPrefix);
|
||||
await RedisClient.HDelAsync(key.GetKey(), key.GetField(CurrentUser.Id!.Value));
|
||||
await Groups.RemoveFromGroupAsync(Context.ConnectionId, ChatConst.AllGroupName);
|
||||
await Clients.All.SendAsync("offlineUser", CurrentUser.Id!.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,6 @@
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
|
||||
<PackageReference Include="Volo.Abp.AspNetCore.SignalR" Version="$(AbpVersion)" />
|
||||
|
||||
<PackageReference Include="Volo.Abp.Ddd.Domain" Version="$(AbpVersion)" />
|
||||
@@ -19,7 +18,6 @@
|
||||
<ItemGroup>
|
||||
<Folder Include="Entities\" />
|
||||
<Folder Include="EventHandlers\" />
|
||||
<Folder Include="Managers\" />
|
||||
<Folder Include="Repositories\" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user