2022-10-03 17:04:59 +08:00
|
|
|
|
using IPTools.Core;
|
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
|
using Microsoft.AspNetCore.SignalR;
|
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2022-09-04 18:47:39 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2022-10-03 17:04:59 +08:00
|
|
|
|
using Yi.Framework.Common.Enum;
|
2022-09-04 18:47:39 +08:00
|
|
|
|
|
|
|
|
|
|
namespace Yi.Framework.WebCore.SignalRHub
|
|
|
|
|
|
{
|
|
|
|
|
|
public class MainHub : Hub
|
|
|
|
|
|
{
|
2022-10-03 18:27:37 +08:00
|
|
|
|
public static readonly List<OnlineUser> clientUsers = new();
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-10-17 18:08:16 +08:00
|
|
|
|
private HttpContext? _httpContext;
|
2022-10-03 17:04:59 +08:00
|
|
|
|
private ILogger<MainHub> _logger;
|
|
|
|
|
|
public MainHub(IHttpContextAccessor httpContextAccessor,ILogger<MainHub> logger)
|
|
|
|
|
|
{
|
|
|
|
|
|
_httpContext = httpContextAccessor.HttpContext;
|
|
|
|
|
|
_logger = logger;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-10-03 18:27:37 +08:00
|
|
|
|
|
2022-10-03 17:04:59 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 成功连接
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
2022-09-04 18:47:39 +08:00
|
|
|
|
public override Task OnConnectedAsync()
|
|
|
|
|
|
{
|
2022-10-17 18:08:16 +08:00
|
|
|
|
var name = _httpContext?.GetUserNameInfo();
|
|
|
|
|
|
var loginUser = _httpContext?.GetLoginLogInfo();
|
2022-10-03 17:04:59 +08:00
|
|
|
|
var user = clientUsers.Any(u => u.ConnnectionId == Context.ConnectionId);
|
|
|
|
|
|
//判断用户是否存在,否则添加集合
|
2022-10-17 18:08:16 +08:00
|
|
|
|
if (!user && (Context.User?.Identity?.IsAuthenticated??false))
|
2022-10-03 17:04:59 +08:00
|
|
|
|
{
|
2022-10-03 18:27:37 +08:00
|
|
|
|
OnlineUser users = new(Context.ConnectionId)
|
2022-10-03 17:04:59 +08:00
|
|
|
|
{
|
2022-10-18 09:01:16 +08:00
|
|
|
|
Browser= loginUser?.Browser,
|
|
|
|
|
|
LoginLocation = loginUser?.LoginLocation,
|
|
|
|
|
|
Ipaddr= loginUser?.LoginIp,
|
2022-10-03 18:27:37 +08:00
|
|
|
|
LoginTime=DateTime.Now,
|
2022-10-18 09:01:16 +08:00
|
|
|
|
Os=loginUser?.Os,
|
2022-10-17 18:08:16 +08:00
|
|
|
|
UserName= name??""
|
2022-10-03 17:04:59 +08:00
|
|
|
|
};
|
|
|
|
|
|
clientUsers.Add(users);
|
|
|
|
|
|
_logger.LogInformation($"{DateTime.Now}:{name},{Context.ConnectionId}连接服务端success,当前已连接{clientUsers.Count}个");
|
|
|
|
|
|
|
|
|
|
|
|
//Clients.All.SendAsync(HubsConstant.MoreNotice, SendNotice());
|
|
|
|
|
|
}
|
|
|
|
|
|
//当有人加入,向全部客户端发送当前总数
|
|
|
|
|
|
Clients.All.SendAsync(HubTypeEnum.onlineNum.ToString(), clientUsers.Count);
|
|
|
|
|
|
//Clients.All.SendAsync(HubsConstant.OnlineUser, clientUsers);
|
2022-09-04 18:47:39 +08:00
|
|
|
|
return base.OnConnectedAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-10-03 17:04:59 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 断开连接
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="exception"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2022-10-17 18:08:16 +08:00
|
|
|
|
public override Task OnDisconnectedAsync(Exception? exception)
|
2022-09-04 18:47:39 +08:00
|
|
|
|
{
|
2022-10-03 17:04:59 +08:00
|
|
|
|
var user = clientUsers.Where(p => p.ConnnectionId == Context.ConnectionId).FirstOrDefault();
|
|
|
|
|
|
//判断用户是否存在,否则添加集合
|
|
|
|
|
|
if (user != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
clientUsers.Remove(user);
|
|
|
|
|
|
Clients.All.SendAsync(HubTypeEnum.onlineNum.ToString(), clientUsers.Count);
|
|
|
|
|
|
//Clients.All.SendAsync(HubsConstant.OnlineUser, clientUsers);
|
2022-10-03 18:27:37 +08:00
|
|
|
|
_logger.LogInformation($"用户{user?.UserName}离开了,当前已连接{clientUsers.Count}个");
|
2022-10-03 17:04:59 +08:00
|
|
|
|
}
|
2022-09-04 18:47:39 +08:00
|
|
|
|
return base.OnDisconnectedAsync(exception);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task SendAllTest(string test)
|
|
|
|
|
|
{
|
|
|
|
|
|
await Clients.All.SendAsync("ReceiveAllInfo", test);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|