X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=server%2Fsockets.js;h=3fb7ea2a8c3fe69f9b1bb12516073f826d31670b;hb=89021f181ac0689bbc785ce0ebd9a910e66352b0;hp=f1853b50bc37af1a4e7740791a35d10cf91cd889;hpb=f854c94f5ba65f1797fd803416e53f057f29f151;p=vchess.git diff --git a/server/sockets.js b/server/sockets.js index f1853b50..3fb7ea2a 100644 --- a/server/sockets.js +++ b/server/sockets.js @@ -13,6 +13,13 @@ function getJsonFromUrl(url) return result; } +// Helper to safe-send some message through a (web-)socket: +function send(socket, message) +{ + if (!!socket && socket.readyState == 1) + socket.send(JSON.stringify(message)); +} + module.exports = function(wss) { // Associative array page --> sid --> tmpId --> socket // "page" is either "/" for hall or "/game/some_gid" for Game, @@ -30,8 +37,7 @@ module.exports = function(wss) { Object.keys(clients[page][k]).forEach(x => { if (k == sid && x == tmpId) return; - clients[page][k][x].send(JSON.stringify(Object.assign( - {code:code, from:sid}, obj))); + send(clients[page][k][x], Object.assign({code:code, from:sid}, obj)); }); }); }; @@ -46,23 +52,18 @@ module.exports = function(wss) { delete clients[page]; } }; - const messageListener = (objtxt) => { - let obj = JSON.parse(objtxt); - if (!!obj.target) + const doDisconnect = () => { + deleteConnexion(); + if (!clients[page] || !clients[page][sid]) { - // Check if receiver is connected, because there may be some lag - // between a client disconnects and another notice. - if (Array.isArray(obj.target)) - { - if (!clients[page][obj.target[0]] || - !clients[page][obj.target[0]][obj.target[1]]) - { - return; - } - } - else if (!clients[page][obj.target]) - return; + // I effectively disconnected from this page: + notifyRoom(page, "disconnect"); + if (page.indexOf("/game/") >= 0) + notifyRoom("/", "gdisconnect", {page:page}); } + }; + const messageListener = (objtxt) => { + let obj = JSON.parse(objtxt); switch (obj.code) { // Wait for "connect" message to notify connection to the room, @@ -77,21 +78,14 @@ module.exports = function(wss) { } case "disconnect": // When page changes: - deleteConnexion(); - if (!clients[page] || !clients[page][sid]) - { - // I effectively disconnected from this page: - notifyRoom(page, "disconnect"); - if (page.indexOf("/game/") >= 0) - notifyRoom("/", "gdisconnect", {page:page}); - } + doDisconnect(); break; case "killme": { // Self multi-connect: manual removal + disconnect const doKill = (pg) => { Object.keys(clients[pg][obj.sid]).forEach(x => { - clients[pg][obj.sid][x].send(JSON.stringify({code: "killed"})); + send(clients[pg][obj.sid][x], {code: "killed"}); }); delete clients[pg][obj.sid]; }; @@ -100,8 +94,7 @@ module.exports = function(wss) { if (k != obj.sid) { Object.keys(clients[pg][k]).forEach(x => { - clients[pg][k][x].send(JSON.stringify(Object.assign( - {code:code, from:obj.sid}, o))); + send(clients[pg][k][x], Object.assign({code:code, from:obj.sid}, o)); }); } }); @@ -125,7 +118,7 @@ module.exports = function(wss) { if (k != sid) sockIds.push(k); }); - socket.send(JSON.stringify({code:"pollclients", sockIds:sockIds})); + send(socket, {code:"pollclients", sockIds:sockIds}); break; } case "pollclientsandgamers": //from Hall @@ -146,7 +139,7 @@ module.exports = function(wss) { }); } }); - socket.send(JSON.stringify({code:"pollclientsandgamers", sockIds:sockIds})); + send(socket, {code:"pollclientsandgamers", sockIds:sockIds}); break; } @@ -158,7 +151,8 @@ module.exports = function(wss) { case "askgame": case "askfullgame": { - const tmpIds = Object.keys(clients[page][obj.target]); + const pg = obj.page || page; //required for askidentity and askgame + const tmpIds = Object.keys(clients[pg][obj.target]); if (obj.target == sid) //targetting myself { const idx_myTmpid = tmpIds.findIndex(x => x == tmpId); @@ -166,8 +160,7 @@ module.exports = function(wss) { tmpIds.splice(idx_myTmpid, 1); } const tmpId_idx = Math.floor(Math.random() * tmpIds.length); - clients[page][obj.target][tmpIds[tmpId_idx]].send( - JSON.stringify({code:obj.code, from:[sid,tmpId]})); + send(clients[pg][obj.target][tmpIds[tmpId_idx]], {code:obj.code, from:[sid,tmpId,page]}); break; } @@ -176,10 +169,7 @@ module.exports = function(wss) { case "startgame": Object.keys(clients[page][obj.target]).forEach(x => { if (obj.target != sid || x != tmpId) - { - clients[page][obj.target][x].send(JSON.stringify( - {code:obj.code, data:obj.data})); - } + send(clients[page][obj.target][x], {code:obj.code, data:obj.data}); }); break; @@ -196,6 +186,11 @@ module.exports = function(wss) { notifyRoom(page, obj.code, {data:obj.data}); break; + case "result": + // Special case: notify all, 'transroom': Game --> Hall + notifyRoom("/", "result", {gid:obj.gid, score:obj.score}); + break; + // Passing, relaying something: from isn't needed, // but target is fully identified (sid + tmpId) case "challenge": @@ -203,14 +198,16 @@ module.exports = function(wss) { case "game": case "identity": case "lastate": - clients[page][obj.target[0]][obj.target[1]].send(JSON.stringify( - {code:obj.code, data:obj.data})); + { + const pg = obj.target[2] || page; //required for identity and game + send(clients[pg][obj.target[0]][obj.target[1]], {code:obj.code, data:obj.data}); break; + } } }; const closeListener = () => { - // For tab or browser closing: - deleteConnexion(); + // For browser or tab closing (including page reload): + doDisconnect(); }; // Update clients object: add new connexion if (!clients[page])