X-Git-Url: https://git.auder.net/css/rpsls.css?a=blobdiff_plain;f=client%2Fsrc%2Fviews%2FMyGames.vue;h=7615308b730bc721ae73c543146279d441a5fbcd;hb=68e3aa8c7a92efe3461bfc5c904f9763bca5d2da;hp=60783941ff005005a2ec1dff3decb7cee385a173;hpb=7617c334b43d2f5fff40cbec72127ed30e5867c0;p=vchess.git diff --git a/client/src/views/MyGames.vue b/client/src/views/MyGames.vue index 60783941..7615308b 100644 --- a/client/src/views/MyGames.vue +++ b/client/src/views/MyGames.vue @@ -58,7 +58,13 @@ export default { connexionString: "" }; }, + watch: { + $route: function(to, from) { + if (to.path != "/mygames") this.cleanBeforeDestroy(); + } + }, created: function() { + window.addEventListener("beforeunload", this.cleanBeforeDestroy); // Initialize connection this.connexionString = params.socketUrl + @@ -132,9 +138,16 @@ export default { }); }, beforeDestroy: function() { - this.conn.send(JSON.stringify({code: "disconnect"})); + this.cleanBeforeDestroy(); }, methods: { + cleanBeforeDestroy: function() { + window.removeEventListener("beforeunload", this.cleanBeforeDestroy); + this.conn.removeEventListener("message", this.socketMessageListener); + this.conn.removeEventListener("close", this.socketCloseListener); + this.conn.send(JSON.stringify({code: "disconnect"})); + this.conn = null; + }, setDisplay: function(type, e) { this.display = type; localStorage.setItem("type-myGames", type); @@ -172,6 +185,7 @@ export default { }); }, socketMessageListener: function(msg) { + if (!this.conn) return; const data = JSON.parse(msg.data); let gamesArrays = { "corr": this.corrGames, @@ -252,16 +266,18 @@ export default { game.players[0].sid == this.st.user.sid ? game.players[1].sid : game.players[0].sid; - this.conn.send( - JSON.stringify( - { - code: "mabort", - gid: game.id, - // NOTE: target might not be online - target: oppsid - } - ) - ); + if (!!this.conn) { + this.conn.send( + JSON.stringify( + { + code: "mabort", + gid: game.id, + // NOTE: target might not be online + target: oppsid + } + ) + ); + } } else if (!game.deletedByWhite || !game.deletedByBlack) { // Set score if game isn't deleted on server: