前端&后端:
互发消息流程打通
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ namespace IM_API.Interface.Services
|
||||
/// <param name="receiverId">接收人</param>
|
||||
/// <param name="dto"></param>
|
||||
/// <returns></returns>
|
||||
Task<bool> SendPrivateMessageAsync(int senderId,int receiverId,MessageBaseDto dto);
|
||||
Task<MessageBaseDto> SendPrivateMessageAsync(int senderId,int receiverId,MessageBaseDto dto);
|
||||
/// <summary>
|
||||
/// 发送群聊消息
|
||||
/// </summary>
|
||||
@@ -19,7 +19,7 @@ namespace IM_API.Interface.Services
|
||||
/// <param name="groupId">接收群id</param>
|
||||
/// <param name="dto"></param>
|
||||
/// <returns></returns>
|
||||
Task<bool> SendGroupMessageAsync(int senderId,int groupId,MessageBaseDto dto);
|
||||
Task<MessageBaseDto> SendGroupMessageAsync(int senderId,int groupId,MessageBaseDto dto);
|
||||
/// <summary>
|
||||
/// 获取消息列表
|
||||
/// </summary>
|
||||
|
||||
@@ -79,7 +79,7 @@ namespace IM_API.Services
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
#region 发送群消息
|
||||
public async Task<bool> SendGroupMessageAsync(int senderId, int groupId, MessageBaseDto dto)
|
||||
public async Task<MessageBaseDto> SendGroupMessageAsync(int senderId, int groupId, MessageBaseDto dto)
|
||||
{
|
||||
//判断群存在
|
||||
var isExist = await _context.Groups.AnyAsync(x => x.Id == groupId);
|
||||
@@ -92,12 +92,12 @@ namespace IM_API.Services
|
||||
message.StreamKey = StreamKeyBuilder.Group(groupId);
|
||||
_context.Messages.Add(message);
|
||||
await _context.SaveChangesAsync();
|
||||
return true;
|
||||
return _mapper.Map<MessageBaseDto>(message);
|
||||
|
||||
}
|
||||
#endregion
|
||||
#region 发送私聊消息
|
||||
public async Task<bool> SendPrivateMessageAsync(int senderId, int receiverId, MessageBaseDto dto)
|
||||
public async Task<MessageBaseDto> SendPrivateMessageAsync(int senderId, int receiverId, MessageBaseDto dto)
|
||||
{
|
||||
bool isExist = await _context.Friends.AnyAsync(x => x.FriendId == receiverId);
|
||||
if (!isExist) throw new BaseException(CodeDefine.FRIEND_RELATION_NOT_FOUND);
|
||||
@@ -107,7 +107,7 @@ namespace IM_API.Services
|
||||
message.StreamKey = StreamKeyBuilder.Private(dto.SenderId, dto.ReceiverId);
|
||||
_context.Messages.Add(message);
|
||||
await _context.SaveChangesAsync();
|
||||
return true;
|
||||
return _mapper.Map<MessageBaseDto>(message);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user