前端:

1、完善创建群聊逻辑
后端:
1、完善群聊相关接口
This commit is contained in:
2026-02-11 22:44:28 +08:00
parent dc6ecf224d
commit 77db20dc38
55 changed files with 1986 additions and 95 deletions
+18 -5
View File
@@ -7,6 +7,7 @@ using IM_API.Models;
using IM_API.Tools;
using IM_API.VOs.Message;
using Microsoft.AspNetCore.SignalR;
using StackExchange.Redis;
using System.Security.Claims;
namespace IM_API.Hubs
@@ -15,14 +16,13 @@ namespace IM_API.Hubs
{
private IMessageSevice _messageService;
private readonly IConversationService _conversationService;
private readonly IEventBus _eventBus;
private readonly IMapper _mapper;
public ChatHub(IMessageSevice messageService, IConversationService conversationService, IEventBus eventBus, IMapper mapper)
private readonly IDatabase _redis;
public ChatHub(IMessageSevice messageService, IConversationService conversationService,
IConnectionMultiplexer connectionMultiplexer)
{
_messageService = messageService;
_conversationService = conversationService;
_eventBus = eventBus;
_mapper = mapper;
_redis = connectionMultiplexer.GetDatabase();
}
public async override Task OnConnectedAsync()
@@ -32,6 +32,7 @@ namespace IM_API.Hubs
Context.Abort();
return;
}
//将用户加入已加入聊天的会话组
string userIdStr = Context.User.FindFirstValue(ClaimTypes.NameIdentifier);
var keys = await _conversationService.GetUserAllStreamKeyAsync(int.Parse(userIdStr));
@@ -39,8 +40,20 @@ namespace IM_API.Hubs
{
await Groups.AddToGroupAsync(Context.ConnectionId, key);
}
//储存用户对应的连接id
await _redis.SetAddAsync(RedisKeys.GetConnectionIdKey(userIdStr), Context.ConnectionId);
await base.OnConnectedAsync();
}
public override async Task OnDisconnectedAsync(Exception? exception)
{
if (!Context.User.Identity.IsAuthenticated)
{
string useridStr = Context.User.FindFirstValue(ClaimTypes.NameIdentifier);
await _redis.SetRemoveAsync(RedisKeys.GetConnectionIdKey(useridStr), Context.ConnectionId);
}
await base.OnDisconnectedAsync(exception);
}
public async Task<HubResponse<MessageBaseVo?>> SendMessage(MessageBaseDto dto)
{
if (!Context.User.Identity.IsAuthenticated)