✨ 新增剔出在线用户
This commit is contained in:
parent
9495589b28
commit
710966e098
@ -1,10 +1,4 @@
|
|||||||
using System;
|
namespace Infrastructure.Constant
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace Infrastructure.Constant
|
|
||||||
{
|
{
|
||||||
public class HubsConstant
|
public class HubsConstant
|
||||||
{
|
{
|
||||||
@ -13,5 +7,7 @@ namespace Infrastructure.Constant
|
|||||||
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";
|
public static string OnlineUser = "onlineUser";
|
||||||
|
public static string LockUser = "lockUser";
|
||||||
|
public static string ConnId = "connId";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -91,7 +91,7 @@ namespace Infrastructure
|
|||||||
|
|
||||||
#region 获取unix时间戳
|
#region 获取unix时间戳
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取unix时间戳
|
/// 获取unix时间戳(毫秒)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="dt"></param>
|
/// <param name="dt"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
|
|||||||
@ -1,8 +1,11 @@
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using Infrastructure.Constant;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.AspNetCore.SignalR;
|
using Microsoft.AspNetCore.SignalR;
|
||||||
using ZR.Admin.WebApi.Filters;
|
using ZR.Admin.WebApi.Filters;
|
||||||
using ZR.Admin.WebApi.Hubs;
|
using ZR.Admin.WebApi.Hubs;
|
||||||
using ZR.Model;
|
using ZR.Model;
|
||||||
|
using ZR.Model.System.Dto;
|
||||||
|
using ZR.Service.System;
|
||||||
|
|
||||||
namespace ZR.Admin.WebApi.Controllers.monitor
|
namespace ZR.Admin.WebApi.Controllers.monitor
|
||||||
{
|
{
|
||||||
@ -12,9 +15,9 @@ namespace ZR.Admin.WebApi.Controllers.monitor
|
|||||||
[ApiExplorerSettings(GroupName = "sys")]
|
[ApiExplorerSettings(GroupName = "sys")]
|
||||||
public class SysUserOnlineController : BaseController
|
public class SysUserOnlineController : BaseController
|
||||||
{
|
{
|
||||||
private IHubContext<Hub> HubContext;
|
private readonly IHubContext<MessageHub> HubContext;
|
||||||
|
|
||||||
public SysUserOnlineController(IHubContext<Hub> hubContext)
|
public SysUserOnlineController(IHubContext<MessageHub> hubContext)
|
||||||
{
|
{
|
||||||
HubContext = hubContext;
|
HubContext = hubContext;
|
||||||
}
|
}
|
||||||
@ -33,5 +36,23 @@ namespace ZR.Admin.WebApi.Controllers.monitor
|
|||||||
|
|
||||||
return SUCCESS(new { result, totalNum = MessageHub.clientUsers.Count });
|
return SUCCESS(new { result, totalNum = MessageHub.clientUsers.Count });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 强退
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpDelete("lock")]
|
||||||
|
[Log(Title = "强退", BusinessType = BusinessType.FORCE)]
|
||||||
|
public async Task<IActionResult> Lock([FromBody] LockUserDto dto)
|
||||||
|
{
|
||||||
|
if (dto == null) { return ToResponse(ResultCode.PARAM_ERROR); }
|
||||||
|
|
||||||
|
await HubContext.Clients.Client(dto.ConnnectionId).SendAsync(HubsConstant.LockUser, new { dto.Reason, dto.Time });
|
||||||
|
|
||||||
|
var expirTime = DateTimeHelper.GetUnixTimeSeconds(DateTime.Now.AddMinutes(dto.Time));
|
||||||
|
|
||||||
|
CacheService.SetLockUser(dto.ClientId, expirTime, dto.Time);
|
||||||
|
return SUCCESS(new { expirTime });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,8 +1,7 @@
|
|||||||
using Infrastructure;
|
using Infrastructure.Constant;
|
||||||
using Infrastructure.Constant;
|
|
||||||
using Infrastructure.Model;
|
|
||||||
using IPTools.Core;
|
using IPTools.Core;
|
||||||
using Microsoft.AspNetCore.SignalR;
|
using Microsoft.AspNetCore.SignalR;
|
||||||
|
using System.Web;
|
||||||
using UAParser;
|
using UAParser;
|
||||||
using ZR.Admin.WebApi.Extensions;
|
using ZR.Admin.WebApi.Extensions;
|
||||||
using ZR.Service.System.IService;
|
using ZR.Service.System.IService;
|
||||||
@ -45,8 +44,11 @@ namespace ZR.Admin.WebApi.Hubs
|
|||||||
|
|
||||||
ClientInfo clientInfo = HttpContextExtension.GetClientInfo(App.HttpContext);
|
ClientInfo clientInfo = HttpContextExtension.GetClientInfo(App.HttpContext);
|
||||||
string device = clientInfo.ToString();
|
string device = clientInfo.ToString();
|
||||||
|
var qs = HttpContextExtension.GetQueryString(App.HttpContext);
|
||||||
|
|
||||||
var userid = HttpContextExtension.GetUId(App.HttpContext);
|
string from = HttpUtility.ParseQueryString(qs).Get("from") ?? "web";
|
||||||
|
|
||||||
|
long userid = HttpContextExtension.GetUId(App.HttpContext);
|
||||||
string uuid = device + userid + ip;
|
string uuid = device + userid + ip;
|
||||||
var user = clientUsers.Any(u => u.ConnnectionId == Context.ConnectionId);
|
var user = clientUsers.Any(u => u.ConnnectionId == Context.ConnectionId);
|
||||||
var user2 = clientUsers.Any(u => u.Uuid == uuid);
|
var user2 = clientUsers.Any(u => u.Uuid == uuid);
|
||||||
@ -54,18 +56,20 @@ namespace ZR.Admin.WebApi.Hubs
|
|||||||
//判断用户是否存在,否则添加集合!user2 && !user &&
|
//判断用户是否存在,否则添加集合!user2 && !user &&
|
||||||
if (!user2 && !user && Context.User.Identity.IsAuthenticated)
|
if (!user2 && !user && Context.User.Identity.IsAuthenticated)
|
||||||
{
|
{
|
||||||
OnlineUsers users = new(Context.ConnectionId, name, userid, ip, device)
|
OnlineUsers onlineUser = new(Context.ConnectionId, name, userid, ip, device)
|
||||||
{
|
{
|
||||||
Location = ip_info.City,
|
Location = ip_info.City,
|
||||||
Uuid = uuid
|
Uuid = uuid,
|
||||||
|
Platform = from
|
||||||
};
|
};
|
||||||
clientUsers.Add(users);
|
clientUsers.Add(onlineUser);
|
||||||
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.Caller.SendAsync(HubsConstant.MoreNotice, SendNotice());
|
||||||
|
Clients.Caller.SendAsync(HubsConstant.ConnId, onlineUser.ConnnectionId);
|
||||||
}
|
}
|
||||||
|
|
||||||
Clients.All.SendAsync(HubsConstant.OnlineNum, clientUsers.Count);
|
Clients.Caller.SendAsync(HubsConstant.OnlineNum, clientUsers.Count);
|
||||||
return base.OnConnectedAsync();
|
return base.OnConnectedAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,7 +94,7 @@ namespace ZR.Admin.WebApi.Hubs
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 注册信息
|
/// 发送信息
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="connectId"></param>
|
/// <param name="connectId"></param>
|
||||||
/// <param name="userName"></param>
|
/// <param name="userName"></param>
|
||||||
@ -103,5 +107,15 @@ namespace ZR.Admin.WebApi.Hubs
|
|||||||
|
|
||||||
await Clients.Client(connectId).SendAsync("receiveChat", new { userName, message });
|
await Clients.Client(connectId).SendAsync("receiveChat", new { userName, message });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取链接id
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HubMethodName("getConnId")]
|
||||||
|
public string GetConnectId()
|
||||||
|
{
|
||||||
|
return Context.ConnectionId;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -26,6 +26,10 @@
|
|||||||
/// 浏览器
|
/// 浏览器
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string Browser { get; set; }
|
public string Browser { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 平台
|
||||||
|
/// </summary>
|
||||||
|
public string Platform { get; set; } = string.Empty;
|
||||||
|
|
||||||
public OnlineUsers(string clientid, string name, long? userid, string userip, string browser)
|
public OnlineUsers(string clientid, string name, long? userid, string userip, string browser)
|
||||||
{
|
{
|
||||||
|
|||||||
10
ZR.Model/System/Dto/LockUserDto.cs
Normal file
10
ZR.Model/System/Dto/LockUserDto.cs
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
namespace ZR.Model.System.Dto
|
||||||
|
{
|
||||||
|
public class LockUserDto
|
||||||
|
{
|
||||||
|
public string ClientId { get; set; }
|
||||||
|
public string ConnnectionId { get; set; }
|
||||||
|
public string Reason { get; set; }
|
||||||
|
public int Time { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -26,5 +26,6 @@ namespace ZR.Model.System.Dto
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public string Uuid { get; set; } = "";
|
public string Uuid { get; set; } = "";
|
||||||
public string LoginIP { get; set; }
|
public string LoginIP { get; set; }
|
||||||
|
public string ClientId { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user