feat: 完善聊天模块
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Yi.Framework.ChatHub.Application.Contracts.Dtos
|
||||
{
|
||||
public class GroupMessageInputDto
|
||||
{
|
||||
/// <summary>
|
||||
/// 消息内容
|
||||
/// </summary>
|
||||
public string Content { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Yi.Framework.ChatHub.Application.Contracts.Dtos
|
||||
{
|
||||
public class PersonalMessageInputDto
|
||||
{
|
||||
/// <summary>
|
||||
/// 用户id
|
||||
/// </summary>
|
||||
public Guid UserId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 消息内容
|
||||
/// </summary>
|
||||
public string Content { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Import Project="..\..\..\common.props" />
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Volo.Abp.Application.Services;
|
||||
using Yi.Framework.ChatHub.Application.Contracts.Dtos;
|
||||
using Yi.Framework.ChatHub.Domain.Managers;
|
||||
using Yi.Framework.ChatHub.Domain.Shared.Model;
|
||||
|
||||
namespace Yi.Framework.ChatHub.Application.Services
|
||||
{
|
||||
public class ChatMessageService : ApplicationService
|
||||
{
|
||||
private UserMessageManager _userMessageManager;
|
||||
public ChatMessageService(UserMessageManager userMessageManager) { _userMessageManager = userMessageManager; }
|
||||
/// <summary>
|
||||
/// 发送个人消息
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpPost("chat-message/personal")]
|
||||
[Authorize]
|
||||
public async Task SendPersonalMessageAsync(PersonalMessageInputDto input)
|
||||
{
|
||||
await _userMessageManager.SendMessageAsync(MessageContext.CreatePersonal(input.Content, input.UserId,CurrentUser.Id!.Value)); ;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 发送群组消息
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpPost("chat-message/group")]
|
||||
[Authorize]
|
||||
public async Task SendGroupMessageAsync(GroupMessageInputDto input)
|
||||
{
|
||||
await _userMessageManager.SendMessageAsync(MessageContext.CreateAll(input.Content, CurrentUser.Id!.Value)); ;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,31 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using FreeRedis;
|
||||
using Mapster;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Mapster;
|
||||
using Volo.Abp.Application.Services;
|
||||
using Volo.Abp.Caching;
|
||||
using Volo.Abp.DependencyInjection;
|
||||
using Yi.Framework.ChatHub.Application.Contracts.Dtos;
|
||||
using Yi.Framework.ChatHub.Domain.Shared.Caches;
|
||||
using Yi.Framework.ChatHub.Domain.Managers;
|
||||
using Yi.Framework.ChatHub.Domain.Shared.Model;
|
||||
|
||||
namespace Yi.Framework.ChatHub.Application.Services
|
||||
{
|
||||
public class ChatUserService : ApplicationService
|
||||
{
|
||||
/// <summary>
|
||||
/// 使用懒加载防止报错
|
||||
/// </summary>
|
||||
private IRedisClient RedisClient => LazyServiceProvider.LazyGetRequiredService<IRedisClient>();
|
||||
private string CacheKeyPrefix => LazyServiceProvider.LazyGetRequiredService<IOptions<AbpDistributedCacheOptions>>().Value.KeyPrefix;
|
||||
public async Task<List<ChatUserGetListOutputDto>> GetListAsync()
|
||||
private UserMessageManager _messageManager;
|
||||
public ChatUserService(UserMessageManager messageManager) { _messageManager = messageManager; }
|
||||
|
||||
public async Task<List<ChatUserModel>> GetListAsync()
|
||||
{
|
||||
var key = new ChatOnlineUserCacheKey(CacheKeyPrefix);
|
||||
var cacheUsers = (await RedisClient.HGetAllAsync(key.GetKey())).Select(x => System.Text.Json.JsonSerializer.Deserialize < ChatOnlineUserCacheItem >( x.Value)).ToList();
|
||||
var output = cacheUsers.Adapt<List<ChatUserGetListOutputDto>>();
|
||||
var userList = await _messageManager.GetAllUserAsync();
|
||||
var output = userList.Adapt<List<ChatUserModel>>();
|
||||
return output;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,5 +9,8 @@ namespace Yi.Framework.ChatHub.Domain.Shared.Consts
|
||||
public class ChatConst
|
||||
{
|
||||
public const string AllGroupName = "all";
|
||||
|
||||
public const string ClientActionReceiveMsg = "receiveMsg";
|
||||
public const string ClientActionUserStatus = "userStatus";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,9 +4,9 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Yi.Framework.ChatHub.Application.Contracts.Dtos
|
||||
namespace Yi.Framework.ChatHub.Domain.Shared.Model
|
||||
{
|
||||
public class ChatUserGetListOutputDto
|
||||
public class ChatUserModel
|
||||
{
|
||||
/// <summary>
|
||||
/// 用户id
|
||||
@@ -22,5 +22,6 @@ namespace Yi.Framework.ChatHub.Application.Contracts.Dtos
|
||||
/// 用户头像
|
||||
/// </summary>
|
||||
public string UserIcon { get; set; }
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Yi.Framework.ChatHub.Domain.Shared.Model
|
||||
{
|
||||
public class MessageContext
|
||||
{
|
||||
public static MessageContext CreatePersonal(string content, Guid userId,Guid sendUserId)
|
||||
{
|
||||
return new MessageContext() { MessageType = MessageType.Personal, Content = content, ReceiveId = userId ,SendUserId= sendUserId };
|
||||
}
|
||||
|
||||
public static MessageContext CreateAll(string content, Guid sendUserId)
|
||||
{
|
||||
return new MessageContext() { MessageType = MessageType.All, Content = content, SendUserId = sendUserId };
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 消息类型
|
||||
/// </summary>
|
||||
[JsonConverter(typeof(JsonStringEnumConverter))]
|
||||
public MessageType MessageType { get; set; }
|
||||
/// <summary>
|
||||
/// 接收者(用户id、群组id)
|
||||
/// </summary>
|
||||
public Guid? ReceiveId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 发送者的用户id
|
||||
/// </summary>
|
||||
public Guid SendUserId { get; set; }
|
||||
/// <summary>
|
||||
/// 消息内容
|
||||
/// </summary>
|
||||
public string Content { get; set; }
|
||||
|
||||
}
|
||||
|
||||
public enum MessageType
|
||||
{
|
||||
|
||||
Personal,
|
||||
Group,
|
||||
All
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,6 @@
|
||||
<ItemGroup>
|
||||
<Folder Include="Enums\" />
|
||||
<Folder Include="Etos\" />
|
||||
<Folder Include="Model\" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
using FreeRedis;
|
||||
using Mapster;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Volo.Abp.Caching;
|
||||
using Volo.Abp.Domain.Services;
|
||||
using Yi.Framework.ChatHub.Domain.Shared.Caches;
|
||||
using Yi.Framework.ChatHub.Domain.Shared.Consts;
|
||||
using Yi.Framework.ChatHub.Domain.Shared.Model;
|
||||
using Yi.Framework.ChatHub.Domain.SignalRHubs;
|
||||
|
||||
namespace Yi.Framework.ChatHub.Domain.Managers
|
||||
{
|
||||
public class UserMessageManager : DomainService
|
||||
{
|
||||
private IHubContext<ChatCenterHub> _hubContext;
|
||||
public UserMessageManager(IHubContext<ChatCenterHub> hubContext)
|
||||
{
|
||||
_hubContext = hubContext;
|
||||
}
|
||||
/// <summary>
|
||||
/// 使用懒加载防止报错
|
||||
/// </summary>
|
||||
private IRedisClient RedisClient => LazyServiceProvider.LazyGetRequiredService<IRedisClient>();
|
||||
private string CacheKeyPrefix => LazyServiceProvider.LazyGetRequiredService<IOptions<AbpDistributedCacheOptions>>().Value.KeyPrefix;
|
||||
|
||||
public async Task SendMessageAsync(MessageContext context)
|
||||
{
|
||||
switch (context.MessageType)
|
||||
{
|
||||
case MessageType.Personal:
|
||||
var userModel =await GetUserAsync(context.ReceiveId.Value);
|
||||
if (userModel is not null)
|
||||
{
|
||||
await _hubContext.Clients.Client(userModel.ClientId).SendAsync(ChatConst.ClientActionReceiveMsg, context.MessageType, context);
|
||||
}
|
||||
break;
|
||||
case MessageType.Group:
|
||||
throw new NotImplementedException();
|
||||
break;
|
||||
case MessageType.All:
|
||||
await _hubContext.Clients.All.SendAsync(ChatConst.ClientActionReceiveMsg, context.MessageType, context);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public async Task<List<ChatOnlineUserCacheItem>> GetAllUserAsync()
|
||||
{
|
||||
var key = new ChatOnlineUserCacheKey(CacheKeyPrefix);
|
||||
var cacheUsers = (await RedisClient.HGetAllAsync(key.GetKey())).Select(x => System.Text.Json.JsonSerializer.Deserialize<ChatOnlineUserCacheItem>(x.Value)).ToList();
|
||||
return cacheUsers;
|
||||
}
|
||||
|
||||
public async Task<ChatOnlineUserCacheItem?> GetUserAsync(Guid userId)
|
||||
{
|
||||
var key = new ChatOnlineUserCacheKey(CacheKeyPrefix);
|
||||
var cacheUser = System.Text.Json.JsonSerializer.Deserialize < ChatOnlineUserCacheItem >( await RedisClient.HGetAsync(key.GetKey(), key.GetField(userId)));
|
||||
return cacheUser;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -5,16 +5,15 @@ using Microsoft.AspNetCore.SignalR;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Volo.Abp.AspNetCore.SignalR;
|
||||
using Volo.Abp.Caching;
|
||||
using Volo.Abp.DependencyInjection;
|
||||
using Yi.Framework.ChatHub.Application.Contracts.Dtos;
|
||||
using Yi.Framework.ChatHub.Domain.Shared.Caches;
|
||||
using Yi.Framework.ChatHub.Domain.Shared.Consts;
|
||||
using Yi.Framework.ChatHub.Domain.Shared.Model;
|
||||
|
||||
namespace Yi.Framework.ChatHub.Application.SignalRHubs
|
||||
namespace Yi.Framework.ChatHub.Domain.SignalRHubs
|
||||
{
|
||||
[HubRoute("/hub/chat")]
|
||||
[Authorize]
|
||||
public class ChatHub : AbpHub
|
||||
public class ChatCenterHub : AbpHub
|
||||
{
|
||||
/// <summary>
|
||||
/// 使用懒加载防止报错
|
||||
@@ -24,7 +23,7 @@ namespace Yi.Framework.ChatHub.Application.SignalRHubs
|
||||
/// 缓存前缀
|
||||
/// </summary>
|
||||
private string CacheKeyPrefix => LazyServiceProvider.LazyGetRequiredService<IOptions<AbpDistributedCacheOptions>>().Value.KeyPrefix;
|
||||
public ChatHub()
|
||||
public ChatCenterHub()
|
||||
{
|
||||
}
|
||||
|
||||
@@ -48,7 +47,7 @@ namespace Yi.Framework.ChatHub.Application.SignalRHubs
|
||||
|
||||
//连接时,还需要去查询用户包含在的群组,将群主全部加入.Todo
|
||||
await Groups.AddToGroupAsync(Context.ConnectionId, ChatConst.AllGroupName);
|
||||
await Clients.All.SendAsync("liveUser", item.Adapt<ChatUserGetListOutputDto>());
|
||||
await Clients.All.SendAsync("liveUser", item.Adapt<ChatUserModel>());
|
||||
|
||||
|
||||
}
|
||||
@@ -7,7 +7,6 @@
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
|
||||
<PackageReference Include="Volo.Abp.AspNetCore.SignalR" Version="$(AbpVersion)" />
|
||||
|
||||
<PackageReference Include="Volo.Abp.Ddd.Domain" Version="$(AbpVersion)" />
|
||||
@@ -19,7 +18,6 @@
|
||||
<ItemGroup>
|
||||
<Folder Include="Entities\" />
|
||||
<Folder Include="EventHandlers\" />
|
||||
<Folder Include="Managers\" />
|
||||
<Folder Include="Repositories\" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user