Skip to content

Commit

Permalink
Close EventSource while programmatically changing route or reloading
Browse files Browse the repository at this point in the history
  • Loading branch information
ndrsllwngr committed Sep 14, 2020
1 parent 4ab5f75 commit 49a322d
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion templates/game.julius
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const digits = [
];

var gameStatus = "";
var src;

// Toggle flag, or reveal cell
const makeMove = (x, y, gameId, event) => {
Expand Down Expand Up @@ -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);
}
},
Expand All @@ -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) {
Expand Down Expand Up @@ -182,14 +191,23 @@ $(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);
if (message.status === "Paused") {
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 });
Expand Down Expand Up @@ -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);
};

Expand Down

0 comments on commit 49a322d

Please sign in to comment.