From c6be4db66c5b70baaafe34d10a77073fd45ef5a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=8D=E5=81=9A=E7=A0=81=E5=86=9C?= <599854767@qq.com> Date: Sun, 5 Jun 2022 13:15:28 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=9C=A8=E7=BA=BF=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E6=96=B0=E5=A2=9E=E7=94=A8=E6=88=B7=E5=9C=B0?= =?UTF-8?q?=E7=90=86=E4=BD=8D=E7=BD=AE=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ZR.Admin.WebApi/Hubs/MessageHub.cs | 13 ++++++++++--- ZR.Admin.WebApi/Hubs/OnlineUsers.cs | 1 + 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/ZR.Admin.WebApi/Hubs/MessageHub.cs b/ZR.Admin.WebApi/Hubs/MessageHub.cs index b5bf330..f549968 100644 --- a/ZR.Admin.WebApi/Hubs/MessageHub.cs +++ b/ZR.Admin.WebApi/Hubs/MessageHub.cs @@ -5,6 +5,7 @@ using System.Threading.Tasks; using Infrastructure; using Infrastructure.Constant; using Infrastructure.Model; +using IPTools.Core; using Microsoft.AspNetCore.SignalR; using ZR.Admin.WebApi.Extensions; using ZR.Admin.WebApi.Framework; @@ -19,7 +20,7 @@ namespace ZR.Admin.WebApi.Hubs //创建用户集合,用于存储所有链接的用户数据 private static readonly List clientUsers = new(); private readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); - private ISysNoticeService SysNoticeService; + private readonly ISysNoticeService SysNoticeService; public MessageHub(ISysNoticeService noticeService) { @@ -43,12 +44,18 @@ namespace ZR.Admin.WebApi.Hubs { var name = Context.User.Identity.Name; var ip = HttpContextExtension.GetClientUserIp(App.HttpContext); + var ip_info = IpTool.Search(ip); + LoginUser loginUser = JwtUtil.GetLoginUser(App.HttpContext); var user = clientUsers.Any(u => u.ConnnectionId == Context.ConnectionId); //判断用户是否存在,否则添加集合 if (!user && Context.User.Identity.IsAuthenticated) { - clientUsers.Add(new OnlineUsers(Context.ConnectionId, name, loginUser?.UserId, ip)); + OnlineUsers users = new(Context.ConnectionId, name, loginUser?.UserId, ip) + { + Location = ip_info.City + }; + clientUsers.Add(users); Console.WriteLine($"{DateTime.Now}:{name},{Context.ConnectionId}连接服务端success,当前已连接{clientUsers.Count}个"); //Clients.All.SendAsync("welcome", $"欢迎您:{name},当前时间:{DateTime.Now}"); Clients.All.SendAsync(HubsConstant.MoreNotice, SendNotice()); @@ -69,10 +76,10 @@ namespace ZR.Admin.WebApi.Hubs //判断用户是否存在,否则添加集合 if (user != null) { - Console.WriteLine($"用户{user?.Name}离开了,当前已连接{clientUsers.Count}个"); clientUsers.Remove(user); Clients.All.SendAsync(HubsConstant.OnlineNum, clientUsers.Count); Clients.All.SendAsync(HubsConstant.OnlineUser, clientUsers); + Console.WriteLine($"用户{user?.Name}离开了,当前已连接{clientUsers.Count}个"); } return base.OnDisconnectedAsync(exception); } diff --git a/ZR.Admin.WebApi/Hubs/OnlineUsers.cs b/ZR.Admin.WebApi/Hubs/OnlineUsers.cs index a6292e0..cbb0c75 100644 --- a/ZR.Admin.WebApi/Hubs/OnlineUsers.cs +++ b/ZR.Admin.WebApi/Hubs/OnlineUsers.cs @@ -17,6 +17,7 @@ namespace ZR.Model public string Name { get; set; } public DateTime LoginTime { get; set; } public string UserIP { get; set; } + public string Location { get; set; } public OnlineUsers(string clientid, string name, long? userid, string userip) { From ef9baadf92932baa614f2096778d85dd6cc65c05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=8D=E5=81=9A=E7=A0=81=E5=86=9C?= <599854767@qq.com> Date: Sun, 5 Jun 2022 14:52:18 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=8D=87=E7=BA=A7=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ZR.Admin.WebApi/ZR.Admin.WebApi.csproj | 8 ++++---- ZR.CodeGenerator/ZR.CodeGenerator.csproj | 2 +- ZR.Common/ZR.Common.csproj | 6 +++--- ZR.Model/ZR.Model.csproj | 4 ++-- ZR.Repository/ZR.Repository.csproj | 2 +- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/ZR.Admin.WebApi/ZR.Admin.WebApi.csproj b/ZR.Admin.WebApi/ZR.Admin.WebApi.csproj index c366c9e..33f4e72 100644 --- a/ZR.Admin.WebApi/ZR.Admin.WebApi.csproj +++ b/ZR.Admin.WebApi/ZR.Admin.WebApi.csproj @@ -14,10 +14,10 @@ - + - + @@ -25,8 +25,8 @@ - - + + diff --git a/ZR.CodeGenerator/ZR.CodeGenerator.csproj b/ZR.CodeGenerator/ZR.CodeGenerator.csproj index a44309b..91c2fde 100644 --- a/ZR.CodeGenerator/ZR.CodeGenerator.csproj +++ b/ZR.CodeGenerator/ZR.CodeGenerator.csproj @@ -12,6 +12,6 @@ - + diff --git a/ZR.Common/ZR.Common.csproj b/ZR.Common/ZR.Common.csproj index eaeea2f..f3df98d 100644 --- a/ZR.Common/ZR.Common.csproj +++ b/ZR.Common/ZR.Common.csproj @@ -6,9 +6,9 @@ - - - + + + diff --git a/ZR.Model/ZR.Model.csproj b/ZR.Model/ZR.Model.csproj index 20edbb3..9650c06 100644 --- a/ZR.Model/ZR.Model.csproj +++ b/ZR.Model/ZR.Model.csproj @@ -5,9 +5,9 @@ - + - + diff --git a/ZR.Repository/ZR.Repository.csproj b/ZR.Repository/ZR.Repository.csproj index eefd262..70bc436 100644 --- a/ZR.Repository/ZR.Repository.csproj +++ b/ZR.Repository/ZR.Repository.csproj @@ -14,7 +14,7 @@ - +