From 49a322df5bd1d86ad78e0f61f97970e7b7798f74 Mon Sep 17 00:00:00 2001 From: Andreas Ellwanger Date: Mon, 14 Sep 2020 09:07:37 +0200 Subject: [PATCH] Close EventSource while programmatically changing route or reloading --- templates/game.julius | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/templates/game.julius b/templates/game.julius index f3560da..015e58a 100644 --- a/templates/game.julius +++ b/templates/game.julius @@ -12,6 +12,7 @@ const digits = [ ]; var gameStatus = ""; +var src; // Toggle flag, or reveal cell const makeMove = (x, y, gameId, event) => { @@ -75,6 +76,10 @@ const resetGame = (gameId) => { console.log("New gameID - Rerouting to new game...", { status }); const newUrl = "/game/" + data.gameId; console.log("Rerouting...", { newUrl }); + if (src) { + console.log("Closing EventSource"); + src.close(); + } window.location.assign(newUrl); } }, @@ -97,6 +102,10 @@ const pauseGame = (gameId) => { console.info("[SUCCESS] PAUSE GAME", { data }); const newUrl = "@{GamesR}"; console.log("Rerouting...", { newUrl }); + if (src) { + console.log("Closing EventSource"); + src.close(); + } window.location.assign(newUrl); }, error: function (data) { @@ -182,7 +191,12 @@ $(document).ready(function () { setFace(); toggleControls(gameStatus); - var src = new EventSource("@{ChannelR gameIdText}"); + src = new EventSource("@{ChannelR gameIdText}"); + src.onerror = function (input) { + console.log({ input }); + console.error("ONERROR!"); + goBack(); + }; src.onmessage = function (input) { // console.log({ input }); var message = JSON.parse(input.data); @@ -190,6 +204,10 @@ $(document).ready(function () { console.info("PAUSED GAME", message.status); const newUrl = "@{GamesR}"; console.log("Rerouting...", { newUrl }); + if (src) { + console.log("Closing EventSource"); + src.close(); + } window.location.assign(newUrl); } console.log({ message }); @@ -262,6 +280,10 @@ const goBack = () => { console.info("GO BACK"); const newUrl = "@{GamesR}"; console.log("Rerouting...", { newUrl }); + if (src) { + console.log("Closing EventSource"); + src.close(); + } window.location.assign(newUrl); };