From cdb34c934db9f75fcc1d749b5dc23d7ea217a289 Mon Sep 17 00:00:00 2001
From: Benjamin Auder <benjamin.auder@somewhere>
Date: Thu, 22 Aug 2019 16:52:41 +0200
Subject: [PATCH] Fix accumulation of socket.close code. Ready to finish
 Game.js and then the website

---
 client/src/store.js       |  5 +++--
 client/src/views/Game.vue | 23 ++++++++++++-----------
 client/src/views/Hall.vue |  3 +--
 3 files changed, 16 insertions(+), 15 deletions(-)

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);
     };
-- 
2.44.0