From: Benjamin Auder <benjamin.auder@somewhere>
Date: Thu, 15 Nov 2018 10:20:40 +0000 (+0100)
Subject: Add a few TODOs to handle invisible disconnections
X-Git-Url: https://git.auder.net/doc/html/assets/pieces/%7B%7B%20pkg.url%20%7D%7D?a=commitdiff_plain;h=0cb758e078f4981d1e5b5209e66ca02f544ec9d5;p=vchess.git

Add a few TODOs to handle invisible disconnections
---

diff --git a/package-lock.json b/package-lock.json
index 73b10dba..88d9fd81 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1897,7 +1897,8 @@
         "ansi-regex": {
           "version": "2.1.1",
           "bundled": true,
-          "dev": true
+          "dev": true,
+          "optional": true
         },
         "aproba": {
           "version": "1.2.0",
@@ -2312,7 +2313,8 @@
         "safe-buffer": {
           "version": "5.1.1",
           "bundled": true,
-          "dev": true
+          "dev": true,
+          "optional": true
         },
         "safer-buffer": {
           "version": "2.1.2",
@@ -2368,6 +2370,7 @@
           "version": "3.0.1",
           "bundled": true,
           "dev": true,
+          "optional": true,
           "requires": {
             "ansi-regex": "^2.0.0"
           }
@@ -2411,12 +2414,14 @@
         "wrappy": {
           "version": "1.0.2",
           "bundled": true,
-          "dev": true
+          "dev": true,
+          "optional": true
         },
         "yallist": {
           "version": "3.0.2",
           "bundled": true,
-          "dev": true
+          "dev": true,
+          "optional": true
         }
       }
     },
diff --git a/public/javascripts/components/game.js b/public/javascripts/components/game.js
index 13381323..20d0f470 100644
--- a/public/javascripts/components/game.js
+++ b/public/javascripts/components/game.js
@@ -526,7 +526,11 @@ Vue.component('my-game', {
 			{
 				if (!this.oppConnected)
 					return; //abort move if opponent is gone
-				this.conn.send(JSON.stringify({code:"newmove", move:move, oppid:this.oppid}));
+				try {
+					this.conn.send(JSON.stringify({code:"newmove", move:move, oppid:this.oppid}));
+				} catch(INVALID_STATE_ERR) {
+					return; //abort also if we lost connection
+				}
 			}
 			new Audio("/sounds/chessmove1.mp3").play();
 			this.vr.play(move, "ingame");
diff --git a/sockets.js b/sockets.js
index 2a43e323..6dbb24a2 100644
--- a/sockets.js
+++ b/sockets.js
@@ -47,7 +47,10 @@ module.exports = function(wss) {
 				switch (obj.code)
 				{
 					case "newmove":
-						clients[page][obj.oppid].send(JSON.stringify({code:"newmove",move:obj.move}));
+						// TODO: adjust with readyState? (+ setTimeout()?) + send opponent (re)disconnect
+						// https://github.com/websockets/ws/blob/master/lib/websocket.js line 313
+						if (!!clients[page][obj.oppid])
+							clients[page][obj.oppid].send(JSON.stringify({code:"newmove",move:obj.move}));
 						break;
 					case "ping":
 						if (!!clients[page][obj.oppid])
@@ -62,6 +65,7 @@ module.exports = function(wss) {
 							delete games[page];
 							const mycolor = Math.random() < 0.5 ? 'w' : 'b';
 							socket.send(JSON.stringify({code:"newgame",fen:fen,oppid:oppId,color:mycolor}));
+							// TODO: check readyState, potential setTimeout()? + send opponent (re)disconnect
 							clients[page][oppId].send(JSON.stringify({code:"newgame",fen:fen,oppid:sid,color:mycolor=="w"?"b":"w"}));
 						}
 						else