Refactor PlayerPresenceController and NotifyHub: enhance online presence tracking and improve connection handling logic

This commit is contained in:
Alexa
2026-06-05 22:54:08 +03:00
parent 3c0dd64293
commit 4d58130acd
3 changed files with 43 additions and 18 deletions
+12 -3
View File
@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.SignalR;
using DorkNet.Server.Data;
using DorkNet.Server.Services;
using System.Security.Claims;
using System.Text.Json;
@@ -40,6 +41,8 @@ namespace DorkNet.Server.Hubs;
public class NotifyHub(
OnlinePresenceService presence,
NotificationService notifications,
DorkNetDbContext db,
ServerSettingsService serverSettings,
ILogger<NotifyHub> logger) : Hub
{
/// <summary>SignalR group name for a given player id. Use this
@@ -111,11 +114,17 @@ public class NotifyHub(
var pid = TryGetPlayerId();
if (pid is long id)
{
await presence.RemoveConnectionAsync(id, Context.ConnectionId);
var wentOffline = await presence.RemoveConnectionAsync(id, Context.ConnectionId);
await Groups.RemoveFromGroupAsync(Context.ConnectionId, GroupForPlayer(id));
if (wentOffline)
{
var friendIds = await RelationshipQueries.EffectiveFriendIdsAsync(db, serverSettings, id);
if (friendIds.Count > 0)
await notifications.PlayerWentOffline(friendIds, id);
}
logger.LogInformation(
"[hub] disconnected player={PlayerId} conn={ConnectionId} error={Error}",
id, Context.ConnectionId, exception?.Message);
"[hub] disconnected player={PlayerId} conn={ConnectionId} wentOffline={WentOffline} error={Error}",
id, Context.ConnectionId, wentOffline, exception?.Message);
}
else
{