后端:
1、事件订阅增加rabbitmq中间件 移动端: 1、完善移动端目录结构和代码框架
This commit is contained in:
+13
@@ -0,0 +1,13 @@
|
||||
using IM_API.Domain.Events;
|
||||
using MassTransit;
|
||||
|
||||
namespace IM_API.Application.EventHandlers.FriendAddHandler
|
||||
{
|
||||
public class FriendAddConversationHandler : IConsumer<FriendAddEvent>
|
||||
{
|
||||
public Task Consume(ConsumeContext<FriendAddEvent> context)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
using IM_API.Domain.Events;
|
||||
using IM_API.Models;
|
||||
using MassTransit;
|
||||
|
||||
namespace IM_API.Application.EventHandlers.FriendAddHandler
|
||||
{
|
||||
public class FriendAddSignalRHandler : IConsumer<FriendAddEvent>
|
||||
{
|
||||
private readonly ImContext _context;
|
||||
public FriendAddSignalRHandler(ImContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public Task Consume(ConsumeContext<FriendAddEvent> context)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
var @event = context.Message;
|
||||
|
||||
var RequestfriendShip = new Friend()
|
||||
{
|
||||
Avatar = @event.ResponseUser.Avatar,
|
||||
UserId = @event.RequestUser.Id,
|
||||
RemarkName = @event.RequestInfo.NickName,
|
||||
Created = @event.RequestInfo.Created,
|
||||
|
||||
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
+12
-7
@@ -7,12 +7,13 @@ using IM_API.Interface.Services;
|
||||
using IM_API.Models;
|
||||
using IM_API.Services;
|
||||
using IM_API.Tools;
|
||||
using MassTransit;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace IM_API.Application.EventHandlers
|
||||
namespace IM_API.Application.EventHandlers.MessageCreatedHandler
|
||||
{
|
||||
public class ConversationEventHandler : IEventHandler<MessageCreatedEvent>
|
||||
public class ConversationEventHandler : IConsumer<MessageCreatedEvent>
|
||||
{
|
||||
private readonly IConversationService _conversationService;
|
||||
private readonly ILogger<ConversationEventHandler> _logger;
|
||||
@@ -30,9 +31,12 @@ namespace IM_API.Application.EventHandlers
|
||||
_context = imContext;
|
||||
_mapper = mapper;
|
||||
}
|
||||
public async Task Handle(MessageCreatedEvent @event)
|
||||
|
||||
public async Task Consume(ConsumeContext<MessageCreatedEvent> context)
|
||||
{
|
||||
if(@event.ChatType == ChatType.PRIVATE)
|
||||
var @event = context.Message;
|
||||
|
||||
if (@event.ChatType == ChatType.PRIVATE)
|
||||
{
|
||||
Conversation? userAConversation = await _context.Conversations.FirstOrDefaultAsync(
|
||||
x => x.UserId == @event.MsgSenderId && x.TargetId == @event.MsgRecipientId
|
||||
@@ -40,9 +44,9 @@ namespace IM_API.Application.EventHandlers
|
||||
Conversation? userBConversation = await _context.Conversations.FirstOrDefaultAsync(
|
||||
x => x.UserId == @event.MsgRecipientId && x.TargetId == @event.MsgSenderId
|
||||
);
|
||||
if(userAConversation is null || userBConversation is null)
|
||||
if (userAConversation is null || userBConversation is null)
|
||||
{
|
||||
_logger.LogError("消息事件更新会话信息失败:{@event}",@event);
|
||||
_logger.LogError("消息事件更新会话信息失败:{@event}", @event);
|
||||
}
|
||||
userAConversation.LastMessage = @event.MessageContent;
|
||||
userAConversation.LastReadMessageId = @event.MessageId;
|
||||
@@ -50,9 +54,10 @@ namespace IM_API.Application.EventHandlers
|
||||
userBConversation.LastMessage = @event.MessageContent;
|
||||
userBConversation.UnreadCount += 1;
|
||||
userBConversation.LastMessageTime = @event.MessageCreated;
|
||||
_context.UpdateRange(userAConversation,userBConversation);
|
||||
_context.UpdateRange(userAConversation, userBConversation);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
+6
-4
@@ -5,11 +5,12 @@ using IM_API.Dtos;
|
||||
using IM_API.Hubs;
|
||||
using IM_API.Models;
|
||||
using IM_API.Tools;
|
||||
using MassTransit;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
|
||||
namespace IM_API.Application.EventHandlers
|
||||
namespace IM_API.Application.EventHandlers.MessageCreatedHandler
|
||||
{
|
||||
public class SignalREventHandler : IEventHandler<MessageCreatedEvent>
|
||||
public class SignalREventHandler : IConsumer<MessageCreatedEvent>
|
||||
{
|
||||
private readonly IHubContext<ChatHub> _hub;
|
||||
private readonly IMapper _mapper;
|
||||
@@ -19,9 +20,10 @@ namespace IM_API.Application.EventHandlers
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
public async Task Handle(MessageCreatedEvent @event)
|
||||
public async Task Consume(ConsumeContext<MessageCreatedEvent> context)
|
||||
{
|
||||
if(@event.ChatType == Models.ChatType.PRIVATE)
|
||||
var @event = context.Message;
|
||||
if (@event.ChatType == Models.ChatType.PRIVATE)
|
||||
{
|
||||
MessageBaseDto messageBaseDto = new MessageBaseDto
|
||||
{
|
||||
@@ -0,0 +1,40 @@
|
||||
using IM_API.Application.EventHandlers.FriendAddHandler;
|
||||
using IM_API.Application.EventHandlers.MessageCreatedHandler;
|
||||
using MassTransit;
|
||||
|
||||
namespace IM_API.Configs
|
||||
{
|
||||
public static class MQConfig
|
||||
{
|
||||
public static IServiceCollection AddRabbitMQ(this IServiceCollection services, RabbitMqOptions options)
|
||||
{
|
||||
services.AddMassTransit(x =>
|
||||
{
|
||||
x.AddConsumer<ConversationEventHandler>();
|
||||
x.AddConsumer<SignalREventHandler>();
|
||||
x.AddConsumer<FriendAddConversationHandler>();
|
||||
x.AddConsumer<FriendAddSignalRHandler>();
|
||||
|
||||
x.UsingRabbitMq((ctx,cfg) =>
|
||||
{
|
||||
cfg.Host(options.Host, "/", h =>
|
||||
{
|
||||
h.Username(options.Username);
|
||||
h.Password(options.Password);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
|
||||
public class RabbitMqOptions
|
||||
{
|
||||
public string Host { get; set; }
|
||||
public int Port { get; set; }
|
||||
public string Username { get; set; }
|
||||
public string Password { get;set; }
|
||||
}
|
||||
}
|
||||
@@ -123,8 +123,6 @@ namespace IM_API.Configs
|
||||
CreateMap<Group, ConversationDto>()
|
||||
.ForMember(dest => dest.TargetAvatar, opt => opt.MapFrom(src => src.Avatar))
|
||||
.ForMember(dest => dest.TargetName, opt => opt.MapFrom(src => src.Name));
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,8 +23,6 @@ namespace IM_API.Configs
|
||||
services.AddTransient<IMessageSevice, MessageService>();
|
||||
services.AddTransient<IConversationService, ConversationService>();
|
||||
services.AddScoped<IEventBus, InMemoryEventBus>();
|
||||
services.AddScoped<IEventHandler<MessageCreatedEvent>, SignalREventHandler>();
|
||||
services.AddScoped<IEventHandler<MessageCreatedEvent>, ConversationEventHandler>();
|
||||
services.AddSingleton<IJWTService, JWTService>();
|
||||
services.AddSingleton<IRefreshTokenService, RedisRefreshTokenService>();
|
||||
return services;
|
||||
|
||||
@@ -59,7 +59,9 @@ namespace IM_API.Controllers
|
||||
/// <param name="dto"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> HandleRequest([FromRoute]int id, [FromBody]FriendRequestHandleDto dto)
|
||||
public async Task<IActionResult> HandleRequest(
|
||||
[FromRoute]int id, [FromBody]FriendRequestHandleDto dto
|
||||
)
|
||||
{
|
||||
await _friendService.HandleFriendRequestAsync(new HandleFriendRequestDto()
|
||||
{
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
using IM_API.Dtos;
|
||||
|
||||
namespace IM_API.Domain.Events
|
||||
{
|
||||
public record FriendAddEvent:DomainEvent
|
||||
{
|
||||
public override string EventType => "IM.FRIENDS_FRIEND_ADD";
|
||||
/// <summary>
|
||||
/// 发起请求用户
|
||||
/// </summary>
|
||||
public UserInfoDto RequestUser { get; set; }
|
||||
/// <summary>
|
||||
/// 接受请求用户
|
||||
/// </summary>
|
||||
public UserInfoDto ResponseUser { get; set; }
|
||||
|
||||
public FriendRequestResDto RequestInfo { get; set; }
|
||||
/// <summary>
|
||||
/// 好友关系创建时间
|
||||
/// </summary>
|
||||
public DateTime Created { get; set; }
|
||||
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoMapper" Version="12.0.1" />
|
||||
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.0" />
|
||||
<PackageReference Include="MassTransit.RabbitMQ" Version="8.5.5" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.21" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.SignalR" Version="1.2.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.21">
|
||||
|
||||
@@ -33,6 +33,8 @@ namespace IM_API
|
||||
//注入redis
|
||||
var redis = ConnectionMultiplexer.Connect(redisConStr);
|
||||
builder.Services.AddSingleton<IConnectionMultiplexer>(redis);
|
||||
|
||||
builder.Services.AddRabbitMQ(configuration.GetSection("RabbitMqOptions").Get<RabbitMqOptions>());
|
||||
|
||||
builder.Services.AddAllService(configuration);
|
||||
|
||||
|
||||
@@ -170,9 +170,7 @@ namespace IM_API.Services
|
||||
throw new BaseException(CodeDefine.ALREADY_FRIENDS);
|
||||
//生成实体
|
||||
var friendRequst = _mapper.Map<FriendRequest>(dto);
|
||||
var friend = _mapper.Map<Friend>(dto);
|
||||
_context.FriendRequests.Add(friendRequst);
|
||||
_context.Friends.Add(friend);
|
||||
await _context.SaveChangesAsync();
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ using IM_API.Exceptions;
|
||||
using IM_API.Interface.Services;
|
||||
using IM_API.Models;
|
||||
using IM_API.Tools;
|
||||
using MassTransit;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace IM_API.Services
|
||||
@@ -15,13 +16,19 @@ namespace IM_API.Services
|
||||
private readonly ImContext _context;
|
||||
private readonly ILogger<MessageService> _logger;
|
||||
private readonly IMapper _mapper;
|
||||
private readonly IEventBus _eventBus;
|
||||
public MessageService(ImContext context, ILogger<MessageService> logger, IMapper mapper, IEventBus eventBus)
|
||||
//废弃,此处已使用rabbitMQ替代
|
||||
//private readonly IEventBus _eventBus;
|
||||
private readonly IPublishEndpoint _endpoint;
|
||||
public MessageService(
|
||||
ImContext context, ILogger<MessageService> logger, IMapper mapper, IEventBus eventBus,
|
||||
IPublishEndpoint publishEndpoint
|
||||
)
|
||||
{
|
||||
_context = context;
|
||||
_logger = logger;
|
||||
_mapper = mapper;
|
||||
_eventBus = eventBus;
|
||||
//_eventBus = eventBus;
|
||||
_endpoint = publishEndpoint;
|
||||
}
|
||||
|
||||
public async Task<List<MessageBaseDto>> GetMessagesAsync(int userId, int conversationId, int? msgId, int? pageSize, bool desc)
|
||||
@@ -93,10 +100,12 @@ namespace IM_API.Services
|
||||
if (!isMember) throw new BaseException(CodeDefine.NO_GROUP_PERMISSION);
|
||||
var message = _mapper.Map<Message>(dto);
|
||||
message.Sender = senderId;
|
||||
message.StreamKey = StreamKeyBuilder.Group(groupId);
|
||||
message.StreamKey = StreamKeyBuilder.Group(
|
||||
|
||||
groupId);
|
||||
_context.Messages.Add(message);
|
||||
await _context.SaveChangesAsync();
|
||||
await _eventBus.PublishAsync(_mapper.Map<MessageCreatedEvent>(message));
|
||||
await _endpoint.Publish(_mapper.Map<MessageCreatedEvent>(message));
|
||||
return _mapper.Map<MessageBaseDto>(message);
|
||||
|
||||
}
|
||||
@@ -112,7 +121,7 @@ namespace IM_API.Services
|
||||
message.StreamKey = StreamKeyBuilder.Private(dto.SenderId, dto.ReceiverId);
|
||||
_context.Messages.Add(message);
|
||||
await _context.SaveChangesAsync();
|
||||
await _eventBus.PublishAsync(_mapper.Map<MessageCreatedEvent>(message));
|
||||
await _endpoint.Publish(_mapper.Map<MessageCreatedEvent>(message));
|
||||
return _mapper.Map<MessageBaseDto>(message);
|
||||
}
|
||||
#endregion
|
||||
|
||||
@@ -16,5 +16,11 @@
|
||||
"ConnectionStrings": {
|
||||
"DefaultConnection": "Server=frp-era.com;Port=26582;Database=IM;User=product;Password=12345678;",
|
||||
"Redis": "192.168.5.100:6379"
|
||||
},
|
||||
"RabbitMQOptions": {
|
||||
"Host": "192.168.5.100",
|
||||
"Port": 5672,
|
||||
"Username": "test",
|
||||
"Password": "123456"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user