Skip to content

Commit

Permalink
Renamed regular fetch to heartbeat
Browse files Browse the repository at this point in the history
  • Loading branch information
derekShaheen committed Apr 7, 2024
1 parent ef7bdb6 commit 1a51f8c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 24 deletions.
41 changes: 23 additions & 18 deletions WickerNetwork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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
{
Expand All @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -342,13 +342,13 @@ <h5 class="modal-title" id="modalLabel">Command Request</h5>
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');
Expand Down Expand Up @@ -447,13 +447,13 @@ <h5 class="modal-title" id="modalLabel">Command Request</h5>
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');
Expand Down Expand Up @@ -582,7 +582,7 @@ <h5 class="modal-title" id="modalLabel">Command Request</h5>
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();
});

</script>
Expand Down

0 comments on commit 1a51f8c

Please sign in to comment.