LANCommander/LANCommander.Server/Hubs/_RpcHub.cs
Pat Hartl ecaecd21bb Send keepalive while game is running
Sends a keepalive/heartbeat signal across RPC while a game is running. Server-side this enables the ability to manage play session state with realtime information instead of hoping that the client will stop the play session. Keepalives are tracked in server cache allowing for future potential horizontally-scaled server instances.
2026-06-14 00:53:45 -05:00

55 lines
No EOL
1.7 KiB
C#

using AutoMapper;
using LANCommander.SDK.Rpc.Client;
using LANCommander.SDK.Rpc.Server;
using LANCommander.SDK.Services;
using LANCommander.Server.Services;
using LANCommander.Server.Services.PowerShell;
using Microsoft.AspNetCore.SignalR;
using ZiggyCreatures.Caching.Fusion;
namespace LANCommander.Server.Hubs;
public partial class RpcHub(
IFusionCache cache,
IMapper mapper,
ILogger<RpcHub> logger,
ServerService serverService,
GameService gameService,
ScriptDebugger scriptDebugger,
ScriptClient scriptClient,
PlaySessionKeepAliveTracker keepAliveTracker) : Hub<IRpcSubscriber>, IRpcHub
{
private string GetConnectionsCacheKey(string userIdentifier) => $"RPC/Connections/{userIdentifier}";
public override async Task OnConnectedAsync()
{
var cacheKey = GetConnectionsCacheKey(Context.UserIdentifier);
var connections = await cache.TryGetAsync<List<string>>(cacheKey);
connections = connections.HasValue
? new List<string>(connections.Value)
: new List<string>();
connections.Value.RemoveAll(c => c == Context.ConnectionId);
connections.Value.Add(Context.ConnectionId);
await cache.SetAsync(cacheKey, connections.Value);
await base.OnConnectedAsync();
}
public override async Task OnDisconnectedAsync(Exception? exception)
{
var cacheKey = GetConnectionsCacheKey(Context.UserIdentifier);
var connections = await cache.TryGetAsync<List<string>>(cacheKey);
if (connections.HasValue)
{
connections.Value.RemoveAll(c => c == Context.ConnectionId);
await cache.SetAsync(cacheKey, connections.Value);
}
}
}