@page "/servers/{wId:int}"
@inject IJSRuntime JS
@rendermode InteractiveServer
Server #@wId
@if (server == null)
{
Loading...
}
else
{
@foreach (var log in logs)
{
@log
}
}
@code {
[Parameter]
public int wId { get; set; }
private World? server;
private List logs = [];
protected override async Task OnInitializedAsync()
{
await GetServer();
}
public async Task GetServer()
{
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(Environment.GetEnvironmentVariable("ADMIN_KEY"));
server = await httpClient.GetFromJsonAsync($"http://localhost:8080/api/admin/servers/{wId}");
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await JS.InvokeVoidAsync("serverLogsStream.start", DotNetObjectReference.Create(this), wId);
}
}
[JSInvokable]
public Task ReceiveLog(string log)
{
logs.Add(log);
InvokeAsync(StateHasChanged);
return Task.CompletedTask;
}
public async Task StartServer()
{
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(Environment.GetEnvironmentVariable("ADMIN_KEY"));
await JS.InvokeVoidAsync("serverLogsStream.start", DotNetObjectReference.Create(this), wId);
await httpClient.PutAsync($"http://localhost:8080/api/admin/servers/{wId}/open", new StringContent(""));
await GetServer();
}
public async Task StopServer()
{
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(Environment.GetEnvironmentVariable("ADMIN_KEY"));
await JS.InvokeVoidAsync("serverLogsStream.stop");
await httpClient.PutAsync($"http://localhost:8080/api/admin/servers/{wId}/close", new StringContent(""));
await GetServer();
}
}