LANCommander/LANCommander.Server/UI/Pages/Servers/Components/Console.razor

176 lines
4.8 KiB
Text
Raw Permalink Normal View History

@using LANCommander.SDK.Enums
@using Microsoft.AspNetCore.SignalR.Client
2023-04-13 17:57:51 -05:00
@using XtermBlazor
@inject ServerConsoleService ServerConsoleService
@inject NavigationManager NavigationManager
@inject IMessageService MessageService
2024-07-07 16:33:46 -05:00
@inject ILogger<Console> Logger
@implements IAsyncDisposable
2023-04-15 00:16:38 -05:00
<Xterm @ref="Terminal" Options="TerminalOptions" AddonIds="TerminalAddons" />
2023-08-28 20:14:50 -05:00
@if (ServerConsole != null && ServerConsole.Type == ServerConsoleType.RCON)
{
<Input @ref="CommandInput" @bind-Value="Command" BindOnInput OnPressEnter="OnPressEnter" @onkeyup="OnCommandKeyDown" />
}
@code {
[Parameter] public Guid ServerId { get; set; }
[Parameter] public Guid ServerConsoleId { get; set; }
2023-04-13 17:57:51 -05:00
Xterm? Terminal;
HubConnection? HubConnection;
ServerConsole ServerConsole;
2023-04-15 00:16:38 -05:00
Input<string> CommandInput;
string Command;
string[] History;
int HistoryPosition;
2023-04-13 17:57:51 -05:00
TerminalOptions TerminalOptions = new TerminalOptions
{
CursorBlink = true,
2023-04-15 00:16:38 -05:00
CursorStyle = CursorStyle.Block
};
string[] TerminalAddons = new string[]
{
"xterm-addon-fit"
2023-04-13 17:57:51 -05:00
};
protected override async Task OnInitializedAsync()
{
2023-08-28 20:14:50 -05:00
if (ServerConsoleId != Guid.Empty)
{
ServerConsole = await ServerConsoleService.GetAsync(ServerConsoleId);
2023-08-28 20:14:50 -05:00
await Connect(ServerConsole);
}
else
{
await Connect();
}
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await Task.Delay(100);
if (Terminal != null)
2024-08-12 00:20:59 -05:00
await Terminal.Addon("xterm-addon-fit").InvokeVoidAsync("fit");
2023-08-28 20:14:50 -05:00
if (ServerConsoleId != Guid.Empty)
{
var log = await ServerConsoleService.ReadLogAsync(ServerConsoleId);
foreach (var line in log)
{
await Terminal.WriteLine(line);
2023-08-28 20:14:50 -05:00
}
}
}
}
async Task Connect()
{
HubConnection = new HubConnectionBuilder()
.WithUrl(NavigationManager.ToAbsoluteUri("/hubs/gameserver"))
.Build();
HubConnection.On<Guid, string>("Log", (serverId, message) =>
{
if (serverId == ServerId)
Terminal.WriteLine(message);
});
2023-08-28 20:14:50 -05:00
await HubConnection.StartAsync();
}
async Task Connect(ServerConsole serverConsole)
{
History = new string[50];
HistoryPosition = 0;
if (ServerConsole.Type == ServerConsoleType.LogFile)
{
HubConnection = new HubConnectionBuilder()
.WithUrl(NavigationManager.ToAbsoluteUri("/hubs/gameserver"))
.Build();
HubConnection.On<Guid, Guid, string>("Log", (serverId, logId, message) =>
{
if (serverId == ServerId && logId == ServerConsoleId)
Terminal.WriteLine(message);
});
await HubConnection.StartAsync();
}
else if (ServerConsole.Type == ServerConsoleType.RCON)
{
2023-08-28 20:14:50 -05:00
try
{
// ServerProcessService.RconConnect(ServerConsole);
}
catch (Exception ex)
{
2024-07-07 16:33:46 -05:00
MessageService.Error($"Could not connect to RCON server: {ex.Message}");
Logger.LogError($"Could not connect to RCON server");
}
}
2023-04-15 00:16:38 -05:00
}
public async Task OnPressEnter() {
// await ServerProcessService.RconSendCommandAsync(Command, ServerConsole);
Array.Copy(History, 0, History, 1, History.Length - 1);
History[0] = Command;
HistoryPosition = -1;
Command = "";
CommandInput.Value = "";
await Task.Yield();
await CommandInput.ValueChanged.InvokeAsync();
await CommandInput.Focus(FocusBehavior.FocusAndClear);
StateHasChanged();
}
public async Task OnCommandKeyDown(KeyboardEventArgs args)
{
switch (args.Key)
{
case "ArrowUp":
if (HistoryPosition < History.Length && History[HistoryPosition + 1] != null)
HistoryPosition++;
Command = History[HistoryPosition];
StateHasChanged();
break;
case "ArrowDown":
if (HistoryPosition >= 0)
{
HistoryPosition--;
if (HistoryPosition >= 0)
Command = History[HistoryPosition];
else
{
Command = "";
}
}
StateHasChanged();
break;
}
}
public async ValueTask DisposeAsync()
{
if (HubConnection is not null)
await HubConnection.DisposeAsync();
}
}