Allow monitoring of stdout

dhcp-server
Pat Hartl 2023-08-28 20:14:50 -05:00
parent 9e5e2d41db
commit 434775623f
3 changed files with 68 additions and 27 deletions

View File

@ -11,7 +11,7 @@
<Xterm @ref="Terminal" Options="TerminalOptions" AddonIds="TerminalAddons" />
@if (ServerConsole.Type == ServerConsoleType.RCON)
@if (ServerConsole != null && ServerConsole.Type == ServerConsoleType.RCON)
{
<Input @ref="CommandInput" @bind-Value="Command" BindOnInput OnPressEnter="OnPressEnter" @onkeyup="OnCommandKeyDown" />
}
@ -41,9 +41,57 @@
};
protected override async Task OnInitializedAsync()
{
if (ServerConsoleId != Guid.Empty)
{
ServerConsole = await ServerConsoleService.Get(ServerConsoleId);
await Connect(ServerConsole);
}
else
{
await Connect();
}
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await Task.Delay(100);
if (Terminal != null)
await Terminal.InvokeAddonFunctionVoidAsync("xterm-addon-fit", "fit");
if (ServerConsoleId != Guid.Empty)
{
var log = await ServerConsoleService.ReadLog(ServerConsoleId);
foreach (var line in log)
{
await Terminal.WriteLine(line);
}
}
}
}
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);
});
await HubConnection.StartAsync();
}
async Task Connect(ServerConsole serverConsole)
{
History = new string[50];
HistoryPosition = 0;
@ -63,7 +111,8 @@
}
else if (ServerConsole.Type == ServerConsoleType.RCON)
{
try {
try
{
ServerProcessService.RconConnect(ServerConsole);
}
catch (Exception ex)
@ -73,24 +122,6 @@
}
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await Task.Delay(100);
if (Terminal != null)
await Terminal.InvokeAddonFunctionVoidAsync("xterm-addon-fit", "fit");
var log = await ServerConsoleService.ReadLog(ServerConsoleId);
foreach (var line in log)
{
await Terminal.WriteLine(line);
}
}
}
public async Task OnPressEnter() {
await ServerProcessService.RconSendCommandAsync(Command, ServerConsole);

View File

@ -1,6 +1,6 @@
@page "/Servers/{id:guid}"
@page "/Servers/{id:guid}/{panel}"
@page "/Servers/{id:guid}/{panel}/{logId:guid}"
@page "/Servers/{id:guid}/{panel}/{logId}"
@page "/Servers/Add"
@using LANCommander.Pages.Servers.Components
@inject GameService GameService
@ -18,9 +18,14 @@
{
<MenuItem RouterLink="@($"/Servers/{Server.Id}/Consoles")">Consoles</MenuItem>
<SubMenu Key="Monitor" Title="Monitor" Disabled="@(Server.ServerConsoles == null || Server.ServerConsoles.Count == 0)">
@if (!Server.UseShellExecute)
{
<MenuItem Key="Console" RouterLink="@($"/Servers/{Server.Id}/Monitor/Console")">Console</MenuItem>
}
@foreach (var log in Server.ServerConsoles)
{
<MenuItem Key="@log.Id.ToString()" RouterLink="@($"/Servers/{Server.Id}/Consoles/{log.Id}")">@log.Name</MenuItem>
<MenuItem Key="@log.Id.ToString()" RouterLink="@($"/Servers/{Server.Id}/Monitor/{log.Id}")">@log.Name</MenuItem>
}
</SubMenu>
<MenuItem RouterLink="@($"/Servers/{Server.Id}/Files")">Files</MenuItem>
@ -107,11 +112,14 @@
</Form>
}
@if (Panel == "Consoles")
@if (Panel == "Monitor")
{
@if (LogId != null && LogId != Guid.Empty)
@if (LogId == "Console") {
<Console ServerId="@Server.Id" />
}
else if (LogId != null && LogId != Guid.Empty.ToString())
{
<Console ServerId="@Server.Id" ServerConsoleId="@LogId" />
<Console ServerId="@Server.Id" ServerConsoleId="@Guid.Parse(LogId)" />
}
else
{
@ -146,7 +154,7 @@
@code {
[Parameter] public Guid Id { get; set; }
[Parameter] public string Panel { get; set; }
[Parameter] public Guid LogId { get; set; }
[Parameter] public string LogId { get; set; }
IEnumerable<Game> Games = new List<Game>();

View File

@ -146,11 +146,13 @@ namespace LANCommander.Services
process.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
{
HubContext.Clients.All.SendAsync("Log", e.Data);
Logger.Info("Game Server {ServerName} ({ServerId}) Info: {Message}", server.Name, server.Id, e.Data);
});
process.ErrorDataReceived += new DataReceivedEventHandler((sender, e) =>
{
HubContext.Clients.All.SendAsync("Log", e.Data);
Logger.Error("Game Server {ServerName} ({ServerId}) Error: {Message}", server.Name, server.Id, e.Data);
});
}