|
@@ -9,8 +9,6 @@ using System.Text.Json;
|
|
|
using System.Threading.Tasks;
|
|
|
using VeloeMinecraftLauncher.Entity.LauncherProfiles;
|
|
|
using VeloeMinecraftLauncher.Utils;
|
|
|
-using Microsoft.AspNetCore.SignalR.Client;
|
|
|
-using VeloeMinecraftLauncher.Entity.McStatus;
|
|
|
using System.Timers;
|
|
|
using System.Reflection;
|
|
|
using Serilog;
|
|
@@ -26,6 +24,7 @@ using System.Threading;
|
|
|
using System.ComponentModel;
|
|
|
using VeloeMinecraftLauncher.Utils.Logger;
|
|
|
using VeloeMinecraftLauncher.Utils.Downloader;
|
|
|
+using McProtocol = VeloeMinecraftLauncher.Utils.McProtocol;
|
|
|
using VeloeMinecraftLauncher.Utils.Starter;
|
|
|
using System.Windows.Input;
|
|
|
|
|
@@ -115,48 +114,15 @@ public class MainWindowViewModel : ViewModelBase
|
|
|
}
|
|
|
try
|
|
|
{
|
|
|
-
|
|
|
- _logger.Debug("Connecting to WebSoket");
|
|
|
- //conection to my servers
|
|
|
-
|
|
|
+ // Create list of my servers
|
|
|
var serverInfo = await Downloader.DownloadAndDeserializeJsonData<Dictionary<string, string[]>>("https://files.veloe.link/launcher/serversInfo.json");
|
|
|
-
|
|
|
if (serverInfo is not null)
|
|
|
{
|
|
|
- _connection = new HubConnectionBuilder()
|
|
|
- .WithUrl("https://monitor.veloe.link/hubs/data")
|
|
|
- .WithAutomaticReconnect()
|
|
|
- .Build();
|
|
|
-
|
|
|
- Func<Exception, Task> reconnecting = ex => Task.Run(() =>
|
|
|
- {
|
|
|
- _logger.Warning("Reconnecting to WebCoket...");
|
|
|
- });
|
|
|
- Func<string, Task> reconnected = str => Task.Run(() =>
|
|
|
- {
|
|
|
- _logger.Warning("Reconnected to WebCoket.");
|
|
|
- foreach (var server in serverInfo)
|
|
|
- {
|
|
|
- _connection.InvokeAsync("ConnectToGroup", server.Key);
|
|
|
- }
|
|
|
- });
|
|
|
-
|
|
|
- _connection.Reconnecting += reconnecting;
|
|
|
- _connection.Reconnected += reconnected;
|
|
|
-
|
|
|
await Dispatcher.UIThread.InvokeAsync(()=>
|
|
|
{
|
|
|
foreach (var server in serverInfo)
|
|
|
ServerPanels.Add(CreateServerPanel(server.Key, server.Value[0], server.Value[1]));
|
|
|
});
|
|
|
-
|
|
|
- await _connection.StartAsync();
|
|
|
-
|
|
|
- foreach (var server in serverInfo)
|
|
|
- {
|
|
|
- await _connection.InvokeAsync("ConnectToGroup", server.Key);
|
|
|
- }
|
|
|
- _logger.Debug("Connected to WebSoket");
|
|
|
}
|
|
|
|
|
|
_consoleOutputTimer.Elapsed += UpdateConsoleOutput;
|
|
@@ -229,9 +195,10 @@ public class MainWindowViewModel : ViewModelBase
|
|
|
_show = show;
|
|
|
}
|
|
|
|
|
|
+ private const int _serverPanelUpdateRelaxDurationMs = 10_000;
|
|
|
+
|
|
|
System.Timers.Timer _consoleOutputTimer = new(250);
|
|
|
|
|
|
- private HubConnection? _connection;
|
|
|
private string _downloadButton = "Download versions";
|
|
|
private string _startButton = "Start Minecraft";
|
|
|
private string _username = string.Empty;
|
|
@@ -805,7 +772,7 @@ public class MainWindowViewModel : ViewModelBase
|
|
|
|
|
|
private ServerPanelModel CreateServerPanel(string name, string address, string clientName)
|
|
|
{
|
|
|
- ServerPanelModel serverPanelModel = new(name, "Wait for update...", "No players.","-/-", address, clientName);
|
|
|
+ ServerPanelModel serverPanelModel = new(name, $"{name}: Updating...", "No players","-/-", address, clientName);
|
|
|
|
|
|
if (SettingsService.Instance.ServerAutoConnectLinks.ContainsKey(name))
|
|
|
serverPanelModel.UserClientName = SettingsService.Instance.ServerAutoConnectLinks[name];
|
|
@@ -820,22 +787,43 @@ public class MainWindowViewModel : ViewModelBase
|
|
|
};
|
|
|
timeoutTimer.Start();
|
|
|
|
|
|
- _connection?.On<string>($"Update{serverPanelModel.Name}", (message) =>
|
|
|
- {
|
|
|
- McStatus? status = JsonSerializer.Deserialize<McStatus>(message, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
|
|
-
|
|
|
- if(status is not null)
|
|
|
+ Task.Run(async () => {
|
|
|
+ while (true)
|
|
|
{
|
|
|
- serverPanelModel.Status = $"{serverPanelModel.Name}: Online";
|
|
|
- serverPanelModel.Players = $"{status.NumPlayers}/{status.MaxPlayers}";
|
|
|
- serverPanelModel.Tip = String.Empty;
|
|
|
- if (UInt16.Parse(status.NumPlayers ?? "0") > 0)
|
|
|
- serverPanelModel.Tip = string.Join('\n',status.Players);
|
|
|
- else
|
|
|
- serverPanelModel.Tip = "No players.";
|
|
|
- }
|
|
|
- timeoutTimer.Stop();
|
|
|
- timeoutTimer.Start();
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var result = await McProtocol::Client.Ping(address);
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var statusResponse = result.StatusResponse;
|
|
|
+
|
|
|
+ serverPanelModel.Status = $"{serverPanelModel.Name}: Online";
|
|
|
+ serverPanelModel.Players = $"{statusResponse?.Players?.Online}/{statusResponse?.Players?.Max}";
|
|
|
+ serverPanelModel.Tip = String.Empty;
|
|
|
+
|
|
|
+ McProtocol::StatusResponsePlayersSample[]? sample = statusResponse?.Players?.Sample;
|
|
|
+ if (sample is not null && sample.Length > 0)
|
|
|
+ {
|
|
|
+ var playerNames = sample.Select(player => player.Name).ToArray();
|
|
|
+ serverPanelModel.Tip = string.Join('\n', playerNames);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ serverPanelModel.Tip = "No players";
|
|
|
+ }
|
|
|
+
|
|
|
+ timeoutTimer.Stop();
|
|
|
+ timeoutTimer.Start();
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ _logger.Error($"Failed to update ServerPanelModel {name} with status response: {ex.Message}{(ex.StackTrace is not null ? "\n" + ex.StackTrace : "")}");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch { }
|
|
|
+
|
|
|
+ await Task.Delay(_serverPanelUpdateRelaxDurationMs);
|
|
|
+ }
|
|
|
});
|
|
|
|
|
|
return serverPanelModel;
|