LANCommander/LANCommander.Server/Logging/GameServerHubConnection.cs
2024-08-04 18:44:33 -05:00

63 lines
1.7 KiB
C#

using LANCommander.Server.Services;
using Microsoft.AspNetCore.SignalR.Client;
using NLog;
namespace LANCommander.Logging
{
public class GameServerHubConnection : IAsyncDisposable
{
private HubConnection? HubConnection;
private string HubUrl;
public GameServerHubConnection(string hubUrl)
{
var settings = SettingService.GetSettings();
HubUrl = hubUrl.Replace("${gdc:PortNumber}", settings.Port.ToString());
}
public async Task Log(Guid serverId, string message)
{
await EnsureConnection();
if (HubConnection != null)
await HubConnection.SendAsync("Log", serverId, message);
}
public async Task EnsureConnection()
{
if (HubConnection == null)
{
HubConnection = new HubConnectionBuilder()
.WithUrl(HubUrl)
.Build();
await HubConnection.StartAsync();
}
else if (HubConnection.State == HubConnectionState.Disconnected)
{
await HubConnection.StartAsync();
}
}
public async ValueTask DisposeAsync()
{
if (HubConnection != null)
{
try
{
await HubConnection.StopAsync();
await HubConnection.DisposeAsync();
}
catch (Exception ex)
{
NLog.Common.InternalLogger.Error(ex, "Exception in LoggingHubConnection.DisposeAsync");
}
finally
{
HubConnection = null;
}
}
}
}
}