2022-05-16 11:30:32 +08:00
|
|
|
|
using Infrastructure;
|
2022-03-01 14:04:21 +08:00
|
|
|
|
using Infrastructure.Constant;
|
2022-02-27 21:11:46 +08:00
|
|
|
|
using Infrastructure.Model;
|
2022-06-08 09:39:37 +08:00
|
|
|
|
using IPTools.Core;
|
2022-02-27 21:11:46 +08:00
|
|
|
|
using Microsoft.AspNetCore.SignalR;
|
2022-05-15 09:23:55 +08:00
|
|
|
|
using ZR.Admin.WebApi.Extensions;
|
|
|
|
|
|
using ZR.Admin.WebApi.Framework;
|
2022-02-27 21:11:46 +08:00
|
|
|
|
using ZR.Model;
|
2022-05-15 09:23:55 +08:00
|
|
|
|
using ZR.Model.System;
|
2022-03-01 14:04:21 +08:00
|
|
|
|
using ZR.Service.System.IService;
|
2022-02-27 21:11:46 +08:00
|
|
|
|
|
|
|
|
|
|
namespace ZR.Admin.WebApi.Hubs
|
|
|
|
|
|
{
|
|
|
|
|
|
public class MessageHub : Hub
|
|
|
|
|
|
{
|
|
|
|
|
|
//创建用户集合,用于存储所有链接的用户数据
|
|
|
|
|
|
private static readonly List<OnlineUsers> clientUsers = new();
|
2022-03-01 14:04:21 +08:00
|
|
|
|
private readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
2022-06-08 09:39:37 +08:00
|
|
|
|
private readonly ISysNoticeService SysNoticeService;
|
2022-05-15 09:23:55 +08:00
|
|
|
|
|
2022-03-01 14:04:21 +08:00
|
|
|
|
public MessageHub(ISysNoticeService noticeService)
|
|
|
|
|
|
{
|
|
|
|
|
|
SysNoticeService = noticeService;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private ApiResult SendNotice()
|
|
|
|
|
|
{
|
|
|
|
|
|
var result = SysNoticeService.GetSysNotices();
|
|
|
|
|
|
|
|
|
|
|
|
return new ApiResult(200, "success", result);
|
|
|
|
|
|
}
|
2022-02-27 21:11:46 +08:00
|
|
|
|
|
|
|
|
|
|
#region 客户端连接
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 客户端连接的时候调用
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public override Task OnConnectedAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
var name = Context.User.Identity.Name;
|
2022-05-22 19:34:57 +08:00
|
|
|
|
var ip = HttpContextExtension.GetClientUserIp(App.HttpContext);
|
2022-06-08 09:39:37 +08:00
|
|
|
|
var ip_info = IpTool.Search(ip);
|
|
|
|
|
|
|
2022-05-15 09:23:55 +08:00
|
|
|
|
LoginUser loginUser = JwtUtil.GetLoginUser(App.HttpContext);
|
2022-02-27 21:11:46 +08:00
|
|
|
|
var user = clientUsers.Any(u => u.ConnnectionId == Context.ConnectionId);
|
|
|
|
|
|
//判断用户是否存在,否则添加集合
|
2022-02-28 18:37:23 +08:00
|
|
|
|
if (!user && Context.User.Identity.IsAuthenticated)
|
2022-02-27 21:11:46 +08:00
|
|
|
|
{
|
2022-06-08 09:39:37 +08:00
|
|
|
|
OnlineUsers users = new(Context.ConnectionId, name, loginUser?.UserId, ip)
|
|
|
|
|
|
{
|
|
|
|
|
|
Location = ip_info.City
|
|
|
|
|
|
};
|
|
|
|
|
|
clientUsers.Add(users);
|
2022-02-28 18:37:23 +08:00
|
|
|
|
Console.WriteLine($"{DateTime.Now}:{name},{Context.ConnectionId}连接服务端success,当前已连接{clientUsers.Count}个");
|
2022-03-03 12:48:15 +08:00
|
|
|
|
//Clients.All.SendAsync("welcome", $"欢迎您:{name},当前时间:{DateTime.Now}");
|
2022-03-01 14:04:21 +08:00
|
|
|
|
Clients.All.SendAsync(HubsConstant.MoreNotice, SendNotice());
|
2022-02-27 21:11:46 +08:00
|
|
|
|
}
|
2022-03-01 14:04:21 +08:00
|
|
|
|
|
|
|
|
|
|
Clients.All.SendAsync(HubsConstant.OnlineNum, clientUsers.Count);
|
2022-05-15 09:23:55 +08:00
|
|
|
|
Clients.All.SendAsync(HubsConstant.OnlineUser, clientUsers);
|
2022-02-27 21:11:46 +08:00
|
|
|
|
return base.OnConnectedAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 连接终止时调用。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public override Task OnDisconnectedAsync(Exception exception)
|
|
|
|
|
|
{
|
|
|
|
|
|
var user = clientUsers.Where(p => p.ConnnectionId == Context.ConnectionId).FirstOrDefault();
|
|
|
|
|
|
//判断用户是否存在,否则添加集合
|
|
|
|
|
|
if (user != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
clientUsers.Remove(user);
|
2022-03-01 14:04:21 +08:00
|
|
|
|
Clients.All.SendAsync(HubsConstant.OnlineNum, clientUsers.Count);
|
2022-05-15 09:23:55 +08:00
|
|
|
|
Clients.All.SendAsync(HubsConstant.OnlineUser, clientUsers);
|
2022-06-08 09:39:37 +08:00
|
|
|
|
Console.WriteLine($"用户{user?.Name}离开了,当前已连接{clientUsers.Count}个");
|
2022-02-27 21:11:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
return base.OnDisconnectedAsync(exception);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|