From 257ac34224bbee1e63a4e4c189428d52d1568b8f Mon Sep 17 00:00:00 2001 From: HueByte Date: Thu, 19 Feb 2026 15:15:09 +0100 Subject: [PATCH] feat: Add heartbeat handling to ServerDirectoryService for connection health checks --- .../Services/ServerDirectoryService.cs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/EchoHub.Server/Services/ServerDirectoryService.cs b/src/EchoHub.Server/Services/ServerDirectoryService.cs index 75b3784..633f5d7 100644 --- a/src/EchoHub.Server/Services/ServerDirectoryService.cs +++ b/src/EchoHub.Server/Services/ServerDirectoryService.cs @@ -61,6 +61,19 @@ public sealed class ServerDirectoryService : BackgroundService { var connectionPermanentlyClosed = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + connection.On("Ping", async () => + { + _logger.LogDebug("Received alive check from directory — sending heartbeat"); + try + { + await connection.InvokeAsync("Heartbeat"); + } + catch (Exception ex) + { + _logger.LogWarning(ex, "Failed to send heartbeat response"); + } + }); + connection.Reconnected += async _ => { _logger.LogInformation("Reconnected to directory — re-registering server"); @@ -156,8 +169,6 @@ public sealed class ServerDirectoryService : BackgroundService continue; var currentCount = _presenceTracker.GetOnlineUserCount(); - if (currentCount == _lastReportedUserCount) - continue; try {