在线用户新增IP显示

This commit is contained in:
不做码农 2022-05-22 19:34:57 +08:00
parent 956b496fa0
commit 58f5de7037
2 changed files with 6 additions and 3 deletions

View File

@ -42,12 +42,13 @@ namespace ZR.Admin.WebApi.Hubs
public override Task OnConnectedAsync()
{
var name = Context.User.Identity.Name;
var ip = HttpContextExtension.GetClientUserIp(App.HttpContext);
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));
clientUsers.Add(new OnlineUsers(Context.ConnectionId, name, loginUser?.UserId, ip));
Console.WriteLine($"{DateTime.Now}{name},{Context.ConnectionId}连接服务端success当前已连接{clientUsers.Count}个");
//Clients.All.SendAsync("welcome", $"欢迎您:{name},当前时间:{DateTime.Now}");
Clients.All.SendAsync(HubsConstant.MoreNotice, SendNotice());

View File

@ -16,13 +16,15 @@ namespace ZR.Model
public long? Userid { get; set; }
public string Name { get; set; }
public DateTime LoginTime { get; set; }
public string UserIP { get; set; }
public OnlineUsers(string clientid, string name, long? userid)
public OnlineUsers(string clientid, string name, long? userid, string? userip)
{
ConnnectionId = clientid;
Name = name;
LoginTime = DateTime.Now;
Userid = userid;
UserIP = userip;
}
}
}