前端:
1、完善创建群聊逻辑 后端: 1、完善群聊相关接口
This commit is contained in:
+21
@@ -0,0 +1,21 @@
|
||||
using IM_API.Domain.Events;
|
||||
using IM_API.Interface.Services;
|
||||
using MassTransit;
|
||||
|
||||
namespace IM_API.Application.EventHandlers.GroupJoinHandler
|
||||
{
|
||||
public class GroupJoinConversationHandler : IConsumer<GroupJoinEvent>
|
||||
{
|
||||
private IConversationService _conversationService;
|
||||
public GroupJoinConversationHandler(IConversationService conversationService)
|
||||
{
|
||||
_conversationService = conversationService;
|
||||
}
|
||||
|
||||
public async Task Consume(ConsumeContext<GroupJoinEvent> context)
|
||||
{
|
||||
var @event = context.Message;
|
||||
await _conversationService.MakeConversationAsync(@event.UserId, @event.GroupId, Models.ChatType.GROUP);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using IM_API.Domain.Events;
|
||||
using IM_API.Interface.Services;
|
||||
using MassTransit;
|
||||
|
||||
namespace IM_API.Application.EventHandlers.GroupJoinHandler
|
||||
{
|
||||
public class GroupJoinDbHandler : IConsumer<GroupJoinEvent>
|
||||
{
|
||||
private readonly IGroupService _groupService;
|
||||
public GroupJoinDbHandler(IGroupService groupService)
|
||||
{
|
||||
_groupService = groupService;
|
||||
}
|
||||
|
||||
public async Task Consume(ConsumeContext<GroupJoinEvent> context)
|
||||
{
|
||||
await _groupService.MakeGroupMemberAsync(context.Message.UserId,
|
||||
context.Message.GroupId, context.Message.IsCreated ?
|
||||
Models.GroupMemberRole.Master : Models.GroupMemberRole.Normal);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
using IM_API.Domain.Events;
|
||||
using IM_API.Dtos;
|
||||
using IM_API.Hubs;
|
||||
using IM_API.Tools;
|
||||
using IM_API.VOs.Group;
|
||||
using MassTransit;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using StackExchange.Redis;
|
||||
|
||||
namespace IM_API.Application.EventHandlers.GroupJoinHandler
|
||||
{
|
||||
public class GroupJoinSignalrHandler : IConsumer<GroupJoinEvent>
|
||||
{
|
||||
private readonly IHubContext<ChatHub> _hub;
|
||||
private readonly IDatabase _redis;
|
||||
public GroupJoinSignalrHandler(IHubContext<ChatHub> hub, IConnectionMultiplexer connectionMultiplexer)
|
||||
{
|
||||
_hub = hub;
|
||||
_redis = connectionMultiplexer.GetDatabase();
|
||||
}
|
||||
|
||||
public async Task Consume(ConsumeContext<GroupJoinEvent> context)
|
||||
{
|
||||
var @event = context.Message;
|
||||
string stramKey = StreamKeyBuilder.Group(@event.GroupId);
|
||||
//将用户加入群组通知
|
||||
var list = await _redis.SetMembersAsync(RedisKeys.GetConnectionIdKey(@event.UserId.ToString()));
|
||||
if(list != null && list.Length > 0)
|
||||
{
|
||||
var tasks = list.Select(connectionId =>
|
||||
_hub.Groups.AddToGroupAsync(connectionId!, stramKey)
|
||||
).ToList();
|
||||
|
||||
await Task.WhenAll(tasks);
|
||||
}
|
||||
//发送通知给群成员
|
||||
var msg = new GroupJoinVo
|
||||
{
|
||||
GroupId = @event.GroupId,
|
||||
UserId = @event.UserId
|
||||
};
|
||||
await _hub.Clients.Group(stramKey).SendAsync("ReceiveMessage",new HubResponse<GroupJoinVo>("Event",msg));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user