Add a few TODOs to handle invisible disconnections
authorBenjamin Auder <benjamin.auder@somewhere>
Thu, 15 Nov 2018 10:20:40 +0000 (11:20 +0100)
committerBenjamin Auder <benjamin.auder@somewhere>
Thu, 15 Nov 2018 10:20:40 +0000 (11:20 +0100)
package-lock.json
public/javascripts/components/game.js
sockets.js

index 73b10db..88d9fd8 100644 (file)
         "ansi-regex": {
           "version": "2.1.1",
           "bundled": true,
-          "dev": true
+          "dev": true,
+          "optional": true
         },
         "aproba": {
           "version": "1.2.0",
         "safe-buffer": {
           "version": "5.1.1",
           "bundled": true,
-          "dev": true
+          "dev": true,
+          "optional": true
         },
         "safer-buffer": {
           "version": "2.1.2",
           "version": "3.0.1",
           "bundled": true,
           "dev": true,
+          "optional": true,
           "requires": {
             "ansi-regex": "^2.0.0"
           }
         "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
         }
       }
     },
index 1338132..20d0f47 100644 (file)
@@ -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");
index 2a43e32..6dbb24a 100644 (file)
@@ -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