feat: 完善聊天模块
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,69 +0,0 @@
|
||||
using FreeRedis;
|
||||
using Mapster;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
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;
|
||||
|
||||
namespace Yi.Framework.ChatHub.Application.SignalRHubs
|
||||
{
|
||||
[HubRoute("/hub/chat")]
|
||||
[Authorize]
|
||||
public class ChatHub : AbpHub
|
||||
{
|
||||
/// <summary>
|
||||
/// 使用懒加载防止报错
|
||||
/// </summary>
|
||||
private IRedisClient RedisClient => LazyServiceProvider.LazyGetRequiredService<IRedisClient>();
|
||||
/// <summary>
|
||||
/// 缓存前缀
|
||||
/// </summary>
|
||||
private string CacheKeyPrefix => LazyServiceProvider.LazyGetRequiredService<IOptions<AbpDistributedCacheOptions>>().Value.KeyPrefix;
|
||||
public ChatHub()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用户进入聊天室
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public override async Task OnConnectedAsync()
|
||||
{
|
||||
var key = new ChatOnlineUserCacheKey(CacheKeyPrefix);
|
||||
var item = new ChatOnlineUserCacheItem()
|
||||
{
|
||||
UserId = CurrentUser.Id!.Value,
|
||||
ClientId = Context.ConnectionId,
|
||||
UserName = CurrentUser.UserName!
|
||||
};
|
||||
|
||||
|
||||
await RedisClient.HSetAsync(key.GetKey(), key.GetField(CurrentUser.Id!.Value), item);
|
||||
|
||||
|
||||
//连接时,还需要去查询用户包含在的群组,将群主全部加入.Todo
|
||||
await Groups.AddToGroupAsync(Context.ConnectionId, ChatConst.AllGroupName);
|
||||
await Clients.All.SendAsync("liveUser", item.Adapt<ChatUserGetListOutputDto>());
|
||||
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用户退出聊天室
|
||||
/// </summary>
|
||||
/// <param name="exception"></param>
|
||||
/// <returns></returns>
|
||||
public async override Task OnDisconnectedAsync(Exception? exception)
|
||||
{
|
||||
var key = new ChatOnlineUserCacheKey(CacheKeyPrefix);
|
||||
await RedisClient.HDelAsync(key.GetKey(), key.GetField(CurrentUser.Id!.Value));
|
||||
await Groups.RemoveFromGroupAsync(Context.ConnectionId, ChatConst.AllGroupName);
|
||||
await Clients.All.SendAsync("offlineUser", CurrentUser.Id!.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user