新增location字段

This commit is contained in:
不做码农 2022-06-08 09:39:37 +08:00
parent 61be09ce6d
commit 973215cf83
2 changed files with 11 additions and 3 deletions

View File

@ -1,6 +1,7 @@
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;
@ -15,7 +16,7 @@ namespace ZR.Admin.WebApi.Hubs
//创建用户集合,用于存储所有链接的用户数据
private static readonly List<OnlineUsers> clientUsers = new();
private readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
private ISysNoticeService SysNoticeService;
private readonly ISysNoticeService SysNoticeService;
public MessageHub(ISysNoticeService noticeService)
{
@ -39,12 +40,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());
@ -65,10 +72,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);
}

View File

@ -17,6 +17,7 @@ namespace ZR.Admin.WebApi.Hubs
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)
{