149 lines
No EOL
4.2 KiB
Text
149 lines
No EOL
4.2 KiB
Text
@using LANCommander.Server.Services.Enums
|
|
@using Microsoft.AspNetCore.SignalR.Client
|
|
@inject ServerService ServerService
|
|
@inject IMessageService MessageService
|
|
@inject INotificationService NotificationService
|
|
@inject NavigationManager NavigationManager
|
|
@implements IAsyncDisposable
|
|
|
|
<Flex Gap="FlexGap.Large" Align="FlexAlign.Center" Wrap="FlexWrap.NoWrap">
|
|
@switch (Status)
|
|
{
|
|
case ServerProcessStatus.Running:
|
|
<Badge Status="BadgeStatus.Success" Text="Running" />
|
|
break;
|
|
|
|
case ServerProcessStatus.Starting:
|
|
<Badge Status="BadgeStatus.Processing" Text="Starting" />
|
|
break;
|
|
|
|
case ServerProcessStatus.Error:
|
|
<Badge Status="BadgeStatus.Error" Text="Error" />
|
|
break;
|
|
|
|
case ServerProcessStatus.Stopped:
|
|
<Badge Status="BadgeStatus.Default" Text="Stopped" />
|
|
break;
|
|
|
|
case ServerProcessStatus.Stopping:
|
|
<Badge Status="BadgeStatus.Error" Text="Stopping" />
|
|
break;
|
|
|
|
default:
|
|
<Badge Status="BadgeStatus.Warning" Text="Retrieving" />
|
|
break;
|
|
}
|
|
|
|
@if (Status != ServerProcessStatus.Running)
|
|
{
|
|
<Button Type="@ButtonType.Primary" OnClick="() => Start()" Disabled="Status != ServerProcessStatus.Stopped && Status != ServerProcessStatus.Error">Start</Button>
|
|
}
|
|
else
|
|
{
|
|
<Popconfirm OnConfirm="() => Stop()" Title="Are you sure you want to kill this server process?">
|
|
<Button Danger Type="@ButtonType.Primary">Stop</Button>
|
|
</Popconfirm>
|
|
}
|
|
</Flex>
|
|
|
|
@code {
|
|
[CascadingParameter] public Dictionary<Guid, ServerProcessStatus> 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<string>("OnConnected", async connectionId =>
|
|
{
|
|
await HubConnection.InvokeAsync("GetStatus", ServerId);
|
|
});
|
|
|
|
HubConnection.On<ServerProcessStatus, Guid>("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();
|
|
}
|
|
} |