改造null

This commit is contained in:
陈淳
2022-10-17 18:08:16 +08:00
parent 16d25fb60d
commit 9d365dbf1e
20 changed files with 117 additions and 42 deletions

View File

@@ -16,7 +16,7 @@ namespace Yi.Framework.WebCore.SignalRHub
public static readonly List<OnlineUser> clientUsers = new();
private HttpContext _httpContext;
private HttpContext? _httpContext;
private ILogger<MainHub> _logger;
public MainHub(IHttpContextAccessor httpContextAccessor,ILogger<MainHub> logger)
{
@@ -32,11 +32,11 @@ namespace Yi.Framework.WebCore.SignalRHub
/// <returns></returns>
public override Task OnConnectedAsync()
{
var name = _httpContext.GetUserNameInfo();
var loginUser = _httpContext.GetLoginLogInfo();
var name = _httpContext?.GetUserNameInfo();
var loginUser = _httpContext?.GetLoginLogInfo();
var user = clientUsers.Any(u => u.ConnnectionId == Context.ConnectionId);
//判断用户是否存在,否则添加集合
if (!user && Context.User.Identity.IsAuthenticated)
if (!user && (Context.User?.Identity?.IsAuthenticated??false))
{
OnlineUser users = new(Context.ConnectionId)
{
@@ -45,7 +45,7 @@ namespace Yi.Framework.WebCore.SignalRHub
Ipaddr= loginUser.LoginIp,
LoginTime=DateTime.Now,
Os=loginUser.Os,
UserName= name
UserName= name??""
};
clientUsers.Add(users);
_logger.LogInformation($"{DateTime.Now}{name},{Context.ConnectionId}连接服务端success当前已连接{clientUsers.Count}个");
@@ -63,7 +63,7 @@ namespace Yi.Framework.WebCore.SignalRHub
/// </summary>
/// <param name="exception"></param>
/// <returns></returns>
public override Task OnDisconnectedAsync(Exception exception)
public override Task OnDisconnectedAsync(Exception? exception)
{
var user = clientUsers.Where(p => p.ConnnectionId == Context.ConnectionId).FirstOrDefault();
//判断用户是否存在,否则添加集合

View File

@@ -19,18 +19,18 @@ namespace Yi.Framework.WebCore.SignalRHub
/// <summary>
/// 客户端连接Id
/// </summary>
public string ConnnectionId { get; }
public string? ConnnectionId { get; }
/// <summary>
/// 用户id
/// </summary>
public long? UserId { get; set; }
public string UserName { get; set; }
public DateTime LoginTime { get; set; }
public string Ipaddr { get; set; }
public string LoginLocation { get; set; }
public string? UserName { get; set; }
public DateTime? LoginTime { get; set; }
public string? Ipaddr { get; set; }
public string? LoginLocation { get; set; }
public string Os { get; set; }
public string Browser { get; set; }
public string? Os { get; set; }
public string? Browser { get; set; }
}