前端&后端:

互发消息流程打通
This commit is contained in:
2026-01-20 20:25:08 +08:00
parent 507788f6a3
commit be621e9ae2
13 changed files with 244 additions and 58 deletions
+13 -3
View File
@@ -1,5 +1,6 @@
using IM_API.Dtos;
using IM_API.Interface.Services;
using IM_API.Models;
using IM_API.Tools;
using Microsoft.AspNetCore.SignalR;
using System.Security.Claims;
@@ -32,7 +33,7 @@ namespace IM_API.Hubs
}
await base.OnConnectedAsync();
}
public async Task SendPrivateMessage(MessageBaseDto dto)
public async Task SendMessage(MessageBaseDto dto)
{
if (!Context.User.Identity.IsAuthenticated)
{
@@ -41,8 +42,17 @@ namespace IM_API.Hubs
return;
}
var userIdStr = Context.User.FindFirstValue(ClaimTypes.NameIdentifier);
await _messageService.SendPrivateMessageAsync(int.Parse(userIdStr),dto.ReceiverId,dto);
await Clients.Users(dto.ReceiverId.ToString()).SendAsync("ReceiveMessage", dto);
MessageBaseDto msgInfo = null;
if(dto.ChatType.ToLower() == ChatType.PRIVATE.ToString().ToLower())
{
msgInfo = await _messageService.SendPrivateMessageAsync(int.Parse(userIdStr), dto.ReceiverId, dto);
}
else
{
msgInfo = await _messageService.SendGroupMessageAsync(int.Parse(userIdStr), dto.ReceiverId, dto);
}
await Clients.Users(dto.ReceiverId.ToString()).SendAsync("ReceiveMessage", msgInfo);
return;
}
}