diff --git a/WickerNetwork.cs b/WickerNetwork.cs index cd9de6e..f8c9060 100644 --- a/WickerNetwork.cs +++ b/WickerNetwork.cs @@ -23,7 +23,7 @@ public class WickerNetwork private CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); private const string COMMANDS_PATH = "/commands"; - private const string GAME_VARIABLES_PATH = "/game-variables"; + private const string HEARTBEAT_PATH = "/heartbeat"; private const string FAVICON_PATH = "/favicon.ico"; private const string INDEX_URL = @"https://raw.githubusercontent.com/derekShaheen/WickerREST/main/web/index.html"; private const string FAVICON_URL = @"https://raw.githubusercontent.com/derekShaheen/WickerREST/main/web/resources/favicon.ico"; @@ -120,24 +120,9 @@ private async void HandleRequests(CancellationToken cancellationToken) { ServeCommandHandlers(response); } - else if (request.Url.AbsolutePath == GAME_VARIABLES_PATH) + else if (request.Url.AbsolutePath == HEARTBEAT_PATH) { - if (Commands.Instance.GameVariableMethods != null && Commands.Instance.GameVariableMethods.Count > 0) - { - - var variableValues = Commands.Instance.GameVariableMethods.Select(kvp => new - { - VariableName = kvp.Key, - Value = kvp.Value?.Invoke()?.ToString() - }).ToDictionary(kvp => kvp.VariableName, kvp => kvp.Value); - - var json = JsonConvert.SerializeObject(variableValues); - SendResponse(response, json, statusCode: 200, contentType: "application/json"); - } - else - { - SendResponse(response, "No game variables found.", statusCode: 200); - } + ServeHeartbeat(response); } else { @@ -162,6 +147,26 @@ private async void HandleRequests(CancellationToken cancellationToken) } } + private void ServeHeartbeat(HttpListenerResponse response) + { + if (Commands.Instance.GameVariableMethods != null && Commands.Instance.GameVariableMethods.Count > 0) + { + + var variableValues = Commands.Instance.GameVariableMethods.Select(kvp => new + { + VariableName = kvp.Key, + Value = kvp.Value?.Invoke()?.ToString() + }).ToDictionary(kvp => kvp.VariableName, kvp => kvp.Value); + + var json = JsonConvert.SerializeObject(variableValues); + SendResponse(response, json, statusCode: 200, contentType: "application/json"); + } + else + { + SendResponse(response, "No game variables found.", statusCode: 200); + } + } + private void ServeCommandHandlers(HttpListenerResponse response) { if (Commands.Instance.CommandHandlers != null && Commands.Instance.CommandHandlers.Count > 0) diff --git a/web/index.html b/web/index.html index c283e98..eaf0d6f 100644 --- a/web/index.html +++ b/web/index.html @@ -342,13 +342,13 @@ let serverAvailable = true; let prevGameVariables = {}; - function fetchGameVariables() { + function fetchHeartbeat() { if (!serverAvailable) { // Attempt to fetch only if the previous flag indicates the server might be available return; } - fetch('/game-variables') + fetch('/heartbeat') .then(response => { if (!response.ok) { throw new Error('Network response was not ok'); @@ -447,13 +447,13 @@ errorLogged = true; // Log the error once } // Implement a retry logic after a delay - setTimeout(fetchGameVariables, 5000); // Retry after 5 seconds + setTimeout(fetchHeartbeat, 5000); // Retry after 5 seconds }); } // Poll game variables - fetchGameVariables(); - setInterval(fetchGameVariables, 2000); + fetchHeartbeat(); + setInterval(fetchHeartbeat, 2000); function updateConnectionStatus(isConnected) { const statusElement = document.getElementById('connectionStatus'); @@ -582,7 +582,7 @@ document.getElementById('columnControlInput').addEventListener('change', function () { const columnCount = this.value; document.cookie = "SameSite=Lax; columnCount=" + columnCount + "; Path=/; max-age=31536000"; // Expires after 1 year - fetchGameVariables(); + fetchHeartbeat(); });