新增在线用户显示
This commit is contained in:
parent
8147df7e49
commit
e4613336f7
@ -12,5 +12,6 @@ namespace Infrastructure.Constant
|
|||||||
public static string ReceiveNotice = V;
|
public static string ReceiveNotice = V;
|
||||||
public static string OnlineNum = "onlineNum";
|
public static string OnlineNum = "onlineNum";
|
||||||
public static string MoreNotice = "moreNotice";
|
public static string MoreNotice = "moreNotice";
|
||||||
|
public static string OnlineUser = "onlineUser";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,10 +2,14 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Infrastructure;
|
||||||
using Infrastructure.Constant;
|
using Infrastructure.Constant;
|
||||||
using Infrastructure.Model;
|
using Infrastructure.Model;
|
||||||
using Microsoft.AspNetCore.SignalR;
|
using Microsoft.AspNetCore.SignalR;
|
||||||
|
using ZR.Admin.WebApi.Extensions;
|
||||||
|
using ZR.Admin.WebApi.Framework;
|
||||||
using ZR.Model;
|
using ZR.Model;
|
||||||
|
using ZR.Model.System;
|
||||||
using ZR.Service.System.IService;
|
using ZR.Service.System.IService;
|
||||||
|
|
||||||
namespace ZR.Admin.WebApi.Hubs
|
namespace ZR.Admin.WebApi.Hubs
|
||||||
@ -38,18 +42,19 @@ namespace ZR.Admin.WebApi.Hubs
|
|||||||
public override Task OnConnectedAsync()
|
public override Task OnConnectedAsync()
|
||||||
{
|
{
|
||||||
var name = Context.User.Identity.Name;
|
var name = Context.User.Identity.Name;
|
||||||
|
LoginUser loginUser = JwtUtil.GetLoginUser(App.HttpContext);
|
||||||
var user = clientUsers.Any(u => u.ConnnectionId == Context.ConnectionId);
|
var user = clientUsers.Any(u => u.ConnnectionId == Context.ConnectionId);
|
||||||
//判断用户是否存在,否则添加集合
|
//判断用户是否存在,否则添加集合
|
||||||
if (!user && Context.User.Identity.IsAuthenticated)
|
if (!user && Context.User.Identity.IsAuthenticated)
|
||||||
{
|
{
|
||||||
clientUsers.Add(new OnlineUsers(Context.ConnectionId, name));
|
clientUsers.Add(new OnlineUsers(Context.ConnectionId, name, loginUser?.UserId));
|
||||||
Console.WriteLine($"{DateTime.Now}:{name},{Context.ConnectionId}连接服务端success,当前已连接{clientUsers.Count}个");
|
Console.WriteLine($"{DateTime.Now}:{name},{Context.ConnectionId}连接服务端success,当前已连接{clientUsers.Count}个");
|
||||||
//Clients.All.SendAsync("welcome", $"欢迎您:{name},当前时间:{DateTime.Now}");
|
//Clients.All.SendAsync("welcome", $"欢迎您:{name},当前时间:{DateTime.Now}");
|
||||||
Clients.All.SendAsync(HubsConstant.MoreNotice, SendNotice());
|
Clients.All.SendAsync(HubsConstant.MoreNotice, SendNotice());
|
||||||
}
|
}
|
||||||
|
|
||||||
Clients.All.SendAsync(HubsConstant.OnlineNum, clientUsers.Count);
|
Clients.All.SendAsync(HubsConstant.OnlineNum, clientUsers.Count);
|
||||||
|
Clients.All.SendAsync(HubsConstant.OnlineUser, clientUsers);
|
||||||
return base.OnConnectedAsync();
|
return base.OnConnectedAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,6 +71,7 @@ namespace ZR.Admin.WebApi.Hubs
|
|||||||
Console.WriteLine($"用户{user?.Name}离开了,当前已连接{clientUsers.Count}个");
|
Console.WriteLine($"用户{user?.Name}离开了,当前已连接{clientUsers.Count}个");
|
||||||
clientUsers.Remove(user);
|
clientUsers.Remove(user);
|
||||||
Clients.All.SendAsync(HubsConstant.OnlineNum, clientUsers.Count);
|
Clients.All.SendAsync(HubsConstant.OnlineNum, clientUsers.Count);
|
||||||
|
Clients.All.SendAsync(HubsConstant.OnlineUser, clientUsers);
|
||||||
}
|
}
|
||||||
return base.OnDisconnectedAsync(exception);
|
return base.OnDisconnectedAsync(exception);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -13,15 +13,16 @@ namespace ZR.Model
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 用户id
|
/// 用户id
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int Userid { get; set; }
|
public long? Userid { get; set; }
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
public DateTime LoginTime { get; set; }
|
public DateTime LoginTime { get; set; }
|
||||||
|
|
||||||
public OnlineUsers(string clientid, string name)
|
public OnlineUsers(string clientid, string name, long? userid)
|
||||||
{
|
{
|
||||||
ConnnectionId = clientid;
|
ConnnectionId = clientid;
|
||||||
Name = name;
|
Name = name;
|
||||||
LoginTime = DateTime.Now;
|
LoginTime = DateTime.Now;
|
||||||
|
Userid = userid;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user