fix reconnection issues maybe
This commit is contained in:
@@ -14,12 +14,14 @@ using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using StackExchange.Redis.Extensions.Core.Abstractions;
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
namespace MareSynchronosServer.Hubs;
|
||||
|
||||
[Authorize(Policy = "Authenticated")]
|
||||
public partial class MareHub : Hub<IMareHub>, IMareHub
|
||||
{
|
||||
private static readonly ConcurrentDictionary<string, string> _userConnections = new(StringComparer.Ordinal);
|
||||
private readonly MareMetrics _mareMetrics;
|
||||
private readonly SystemInfoService _systemInfoService;
|
||||
private readonly IHttpContextAccessor _contextAccessor;
|
||||
@@ -131,22 +133,37 @@ public partial class MareHub : Hub<IMareHub>, IMareHub
|
||||
|
||||
[Authorize(Policy = "Authenticated")]
|
||||
public override async Task OnConnectedAsync()
|
||||
{
|
||||
if (_userConnections.TryGetValue(UserUID, out var oldId))
|
||||
{
|
||||
_logger.LogCallWarning(MareHubLogger.Args(_contextAccessor.GetIpAddress(), "UpdatingId", oldId, Context.ConnectionId));
|
||||
_userConnections[UserUID] = Context.ConnectionId;
|
||||
}
|
||||
else
|
||||
{
|
||||
_mareMetrics.IncGaugeWithLabels(MetricsAPI.GaugeConnections, labels: Continent);
|
||||
|
||||
try
|
||||
{
|
||||
_logger.LogCallInfo(MareHubLogger.Args(_contextAccessor.GetIpAddress(), UserCharaIdent));
|
||||
_logger.LogCallInfo(MareHubLogger.Args(_contextAccessor.GetIpAddress(), Context.ConnectionId, UserCharaIdent));
|
||||
await _onlineSyncedPairCacheService.InitPlayer(UserUID).ConfigureAwait(false);
|
||||
await UpdateUserOnRedis().ConfigureAwait(false);
|
||||
_userConnections[UserUID] = Context.ConnectionId;
|
||||
}
|
||||
catch
|
||||
{
|
||||
_userConnections.Remove(UserUID, out _);
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
|
||||
await base.OnConnectedAsync().ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Authorize(Policy = "Authenticated")]
|
||||
public override async Task OnDisconnectedAsync(Exception exception)
|
||||
{
|
||||
if (_userConnections.TryGetValue(UserUID, out var connectionId)
|
||||
&& string.Equals(connectionId, Context.ConnectionId, StringComparison.Ordinal))
|
||||
{
|
||||
_mareMetrics.DecGaugeWithLabels(MetricsAPI.GaugeConnections, labels: Continent);
|
||||
|
||||
@@ -154,9 +171,9 @@ public partial class MareHub : Hub<IMareHub>, IMareHub
|
||||
{
|
||||
await _onlineSyncedPairCacheService.DisposePlayer(UserUID).ConfigureAwait(false);
|
||||
|
||||
_logger.LogCallInfo(MareHubLogger.Args(_contextAccessor.GetIpAddress(), UserCharaIdent));
|
||||
_logger.LogCallInfo(MareHubLogger.Args(_contextAccessor.GetIpAddress(), Context.ConnectionId, UserCharaIdent));
|
||||
if (exception != null)
|
||||
_logger.LogCallWarning(MareHubLogger.Args(_contextAccessor.GetIpAddress(), exception.Message, exception.StackTrace));
|
||||
_logger.LogCallWarning(MareHubLogger.Args(_contextAccessor.GetIpAddress(), Context.ConnectionId, exception.Message, exception.StackTrace));
|
||||
|
||||
await RemoveUserFromRedis().ConfigureAwait(false);
|
||||
|
||||
@@ -169,6 +186,15 @@ public partial class MareHub : Hub<IMareHub>, IMareHub
|
||||
|
||||
}
|
||||
catch { }
|
||||
finally
|
||||
{
|
||||
_userConnections.Remove(UserUID, out _);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogCallWarning(MareHubLogger.Args(_contextAccessor.GetIpAddress(), "ObsoleteId", UserUID, Context.ConnectionId));
|
||||
}
|
||||
|
||||
await base.OnDisconnectedAsync(exception).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user