From: Benjamin Auder Date: Thu, 22 Aug 2019 14:52:41 +0000 (+0200) Subject: Fix accumulation of socket.close code. Ready to finish Game.js and then the website X-Git-Url: https://git.auder.net/?p=vchess.git;a=commitdiff_plain;h=cdb34c934db9f75fcc1d749b5dc23d7ea217a289 Fix accumulation of socket.close code. Ready to finish Game.js and then the website --- diff --git a/client/src/store.js b/client/src/store.js index 070b22cc..a999b0ac 100644 --- a/client/src/store.js +++ b/client/src/store.js @@ -13,6 +13,7 @@ export const store = settings: {}, lang: "", }, + socketCloseListener: null, initialize() { ajax("/variants", "GET", res => { this.state.variants = res.variantArray; }); let mysid = localStorage["mysid"]; @@ -48,10 +49,10 @@ export const store = highlight: !!eval(localStorage["highlight"]), sqSize: parseInt(localStorage["sqSize"]), }; - const socketCloseListener = () => { + this.socketCloseListener = () => { this.state.conn = new WebSocket(params.socketUrl + "/?sid=" + mysid); }; - this.state.conn.onclose = socketCloseListener; + this.state.conn.onclose = this.socketCloseListener; const supportedLangs = ["en","es","fr"]; this.state.lang = localStorage["lang"] || supportedLangs.includes(navigator.language) diff --git a/client/src/views/Game.vue b/client/src/views/Game.vue index e3a2f6e0..d15e307e 100644 --- a/client/src/views/Game.vue +++ b/client/src/views/Game.vue @@ -113,7 +113,17 @@ export default { this.gameRef.rid = this.$route.query["rid"]; this.loadGame(); } - const socketMessageListener = msg => { + // TODO: onopen, ask lastState informations + update observers and players status + const socketCloseListener = () => { + store.socketCloseListener(); //reinitialize connexion (in store.js) + this.st.conn.addEventListener('message', socketMessageListener); + this.st.conn.addEventListener('close', socketCloseListener); + }; + this.st.conn.onmessage = this.socketMessageListener; + this.st.conn.onclose = socketCloseListener; + }, + methods: { + socketMessageListener: function(msg) { const data = JSON.parse(msg.data); switch (data.code) { @@ -214,16 +224,7 @@ export default { } break; } - }; - // TODO: onopen, ask lastState informations + update observers and players status - const socketCloseListener = () => { - this.st.conn.addEventListener('message', socketMessageListener); - this.st.conn.addEventListener('close', socketCloseListener); - }; - this.st.conn.onmessage = socketMessageListener; - this.st.conn.onclose = socketCloseListener; - }, - methods: { + }, offerDraw: function() { // TODO: also for corr games if (this.drawOffer == "received") diff --git a/client/src/views/Hall.vue b/client/src/views/Hall.vue index b140e612..a16b5b53 100644 --- a/client/src/views/Hall.vue +++ b/client/src/views/Hall.vue @@ -154,9 +154,8 @@ export default { else //socket not ready yet (initial loading) this.st.conn.onopen = funcPollClients; this.st.conn.onmessage = this.socketMessageListener; - const oldOnclose = this.st.conn.onclose; const socketCloseListener = () => { - oldOnclose(); //reinitialize connexion (in store.js) + store.socketCloseListener(); //reinitialize connexion (in store.js) this.st.conn.addEventListener('message', this.socketMessageListener); this.st.conn.addEventListener('close', socketCloseListener); };