@using LANCommander.Server.Services.Enums @using Microsoft.AspNetCore.SignalR.Client @inject ServerService ServerService @inject IMessageService MessageService @inject INotificationService NotificationService @inject NavigationManager NavigationManager @implements IAsyncDisposable @switch (Status) { case ServerProcessStatus.Running: break; case ServerProcessStatus.Starting: break; case ServerProcessStatus.Error: break; case ServerProcessStatus.Stopped: break; case ServerProcessStatus.Stopping: break; default: break; } @if (Status != ServerProcessStatus.Running) { } else { } @code { [CascadingParameter] public Dictionary CurrentServerStatus { get; set; } = new(); [Parameter] public Guid ServerId { get; set; } HubConnection? HubConnection; Server Server; Timer Timer; ServerProcessStatus Status = ServerProcessStatus.Retrieving; protected override async Task OnInitializedAsync() { await Connect(); } protected override async Task OnParametersSetAsync() { if (!CurrentServerStatus.ContainsKey(ServerId)) Status = ServerProcessStatus.Stopped; else Status = CurrentServerStatus[ServerId]; await Task.Yield(); await InvokeAsync(StateHasChanged); } async Task Connect() { HubConnection = new HubConnectionBuilder() .WithUrl(NavigationManager.ToAbsoluteUri("/hubs/gameserver")) .WithAutomaticReconnect() .Build(); HubConnection.On("OnConnected", async connectionId => { await HubConnection.InvokeAsync("GetStatus", ServerId); }); HubConnection.On("StatusUpdate", (status, serverId) => { if (ServerId == serverId && Status != status) { Status = status; InvokeAsync(StateHasChanged); } }); _ = Task.Run(async () => { try { while (true) { await Task.Delay(1000); if (HubConnection.State == HubConnectionState.Connected) await HubConnection.SendAsync("UpdateStatusAsync", ServerId.ToString()); } } catch { } }); try { await HubConnection.StartAsync(); } catch { } } async Task Start() { if (HubConnection != null && HubConnection.State == HubConnectionState.Connected) { Status = ServerProcessStatus.Starting; await Task.Yield(); await HubConnection.InvokeAsync("StartServer", ServerId); } } async Task Stop() { if (HubConnection != null && HubConnection.State == HubConnectionState.Connected) { Status = ServerProcessStatus.Stopping; await Task.Yield(); await HubConnection.InvokeAsync("StopServer", ServerId); } } public async ValueTask DisposeAsync() { if (HubConnection is not null) await HubConnection.DisposeAsync(); } }