⚡优化signalr消息发送接收
This commit is contained in:
parent
31980c80bb
commit
945f64e46a
@ -3,7 +3,6 @@ using Infrastructure.Converter;
|
|||||||
using Microsoft.AspNetCore.DataProtection;
|
using Microsoft.AspNetCore.DataProtection;
|
||||||
using NLog.Web;
|
using NLog.Web;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Text.Json.Serialization;
|
|
||||||
using ZR.Admin.WebApi.Extensions;
|
using ZR.Admin.WebApi.Extensions;
|
||||||
using ZR.Common.Cache;
|
using ZR.Common.Cache;
|
||||||
using ZR.Infrastructure.WebExtensions;
|
using ZR.Infrastructure.WebExtensions;
|
||||||
|
|||||||
31
ZR.ServiceCore/Model/Dto/ChatMessageDto.cs
Normal file
31
ZR.ServiceCore/Model/Dto/ChatMessageDto.cs
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
namespace ZR.ServiceCore.Model.Dto
|
||||||
|
{
|
||||||
|
public class ChatMessageDto
|
||||||
|
{
|
||||||
|
public ChatUserDto FromUser { get; set; }
|
||||||
|
public bool Self { get; set; }
|
||||||
|
[SugarColumn(IsPrimaryKey = true)]
|
||||||
|
public string ChatId { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 0、文本 1、图片 2、文件 3、语音 4、视频
|
||||||
|
/// </summary>
|
||||||
|
public int MsgType { get; set; }
|
||||||
|
public string StoredKey { get; set; }
|
||||||
|
public long UserId { get; set; }
|
||||||
|
public long ToUserId { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 消息内容,如果type=1/2/3/4,此属性表示文件的URL地址
|
||||||
|
/// </summary>
|
||||||
|
public string Message { get; set; }
|
||||||
|
public long ChatTime { get; set; }
|
||||||
|
public int Online { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class ChatUserDto
|
||||||
|
{
|
||||||
|
public string UserName { get; set; }
|
||||||
|
public string NickName { get; set; }
|
||||||
|
public string Avatar { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,10 +1,12 @@
|
|||||||
using Infrastructure;
|
using Infrastructure;
|
||||||
using Infrastructure.Model;
|
using Infrastructure.Model;
|
||||||
using IPTools.Core;
|
using IPTools.Core;
|
||||||
|
using Mapster;
|
||||||
using Microsoft.AspNetCore.SignalR;
|
using Microsoft.AspNetCore.SignalR;
|
||||||
using System.Web;
|
using System.Web;
|
||||||
using UAParser;
|
using UAParser;
|
||||||
using ZR.Service.System.IService;
|
using ZR.Service.System.IService;
|
||||||
|
using ZR.ServiceCore.Model.Dto;
|
||||||
|
|
||||||
namespace ZR.ServiceCore.Signalr
|
namespace ZR.ServiceCore.Signalr
|
||||||
{
|
{
|
||||||
@ -18,10 +20,12 @@ namespace ZR.ServiceCore.Signalr
|
|||||||
public static List<OnlineUsers> users = new();
|
public static List<OnlineUsers> users = new();
|
||||||
//private readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
//private readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||||
private readonly ISysNoticeService SysNoticeService;
|
private readonly ISysNoticeService SysNoticeService;
|
||||||
|
private readonly ISysUserService UserService;
|
||||||
|
|
||||||
public MessageHub(ISysNoticeService noticeService)
|
public MessageHub(ISysNoticeService noticeService, ISysUserService userService)
|
||||||
{
|
{
|
||||||
SysNoticeService = noticeService;
|
SysNoticeService = noticeService;
|
||||||
|
UserService = userService;
|
||||||
}
|
}
|
||||||
|
|
||||||
private ApiResult SendNotice()
|
private ApiResult SendNotice()
|
||||||
@ -137,12 +141,11 @@ namespace ZR.ServiceCore.Signalr
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 发送信息
|
/// 发送信息
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="toConnectId">对方链接id</param>
|
|
||||||
/// <param name="toUserId"></param>
|
/// <param name="toUserId"></param>
|
||||||
/// <param name="message"></param>
|
/// <param name="message"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HubMethodName("sendMessage")]
|
[HubMethodName("sendMessage")]
|
||||||
public async Task SendMessage(string toConnectId, long toUserId, string message)
|
public async Task SendMessage(long toUserId, string message)
|
||||||
{
|
{
|
||||||
var userName = HttpContextExtension.GetName(App.HttpContext);
|
var userName = HttpContextExtension.GetName(App.HttpContext);
|
||||||
long userid = HttpContextExtension.GetUId(App.HttpContext);
|
long userid = HttpContextExtension.GetUId(App.HttpContext);
|
||||||
@ -150,28 +153,33 @@ namespace ZR.ServiceCore.Signalr
|
|||||||
var toUserInfo = toUserList.FirstOrDefault();
|
var toUserInfo = toUserList.FirstOrDefault();
|
||||||
IList<string> sendToUser = toUserList.Select(x => x.ConnnectionId).ToList();
|
IList<string> sendToUser = toUserList.Select(x => x.ConnnectionId).ToList();
|
||||||
sendToUser.Add(GetConnectId());
|
sendToUser.Add(GetConnectId());
|
||||||
if (toUserInfo != null)
|
var fromUser = await UserService.GetByIdAsync(userid);
|
||||||
|
|
||||||
|
ChatMessageDto messageDto = new()
|
||||||
{
|
{
|
||||||
await Clients.Clients(sendToUser)
|
MsgType = 0,
|
||||||
.SendAsync("receiveChat", new
|
StoredKey = $"{userid}-{toUserId}",
|
||||||
{
|
UserId = userid,
|
||||||
msgType = 0,//文本
|
ChatId = Guid.NewGuid().ToString(),
|
||||||
chatid = Guid.NewGuid().ToString(),
|
ToUserId = toUserId,
|
||||||
userName,
|
Message = message,
|
||||||
userid,
|
Online = 1,
|
||||||
toUserName = toUserInfo.Name,
|
ChatTime = DateTimeHelper.GetUnixTimeSeconds(DateTime.Now),
|
||||||
toUserid = toUserInfo.Userid,
|
FromUser = fromUser.Adapt<ChatUserDto>(),
|
||||||
message,
|
};
|
||||||
chatTime = DateTime.Now
|
if (toUserInfo == null)
|
||||||
});
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
|
messageDto.Online = 0;
|
||||||
//TODO 存储离线消息
|
//TODO 存储离线消息
|
||||||
Console.WriteLine($"{toUserId}不在线");
|
Console.WriteLine($"{toUserId}不在线");
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
await Clients.Clients(sendToUser)
|
||||||
|
.SendAsync("receiveChat", messageDto);
|
||||||
|
}
|
||||||
|
|
||||||
Console.WriteLine($"用户{userName}对{toConnectId}-{toUserId}说:{message}");
|
Console.WriteLine($"用户{userName}对{toUserId}说:{message}");
|
||||||
}
|
}
|
||||||
|
|
||||||
private OnlineUsers GetUserByConnId(string connId)
|
private OnlineUsers GetUserByConnId(string connId)
|
||||||
|
|||||||
@ -17,7 +17,7 @@ namespace ZR.ServiceCore.SqlSugar
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="services"></param>
|
/// <param name="services"></param>
|
||||||
/// <param name="environment"></param>
|
/// <param name="environment"></param>
|
||||||
public static void AddDb(this IServiceCollection services,IWebHostEnvironment environment)
|
public static void AddDb(this IServiceCollection services, IWebHostEnvironment environment)
|
||||||
{
|
{
|
||||||
var options = App.OptionsSetting;
|
var options = App.OptionsSetting;
|
||||||
List<DbConfigs> dbConfigs = options.DbConfigs;
|
List<DbConfigs> dbConfigs = options.DbConfigs;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user