X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=server%2Fsockets.js;h=42a8840fc51fb014e593c6312b9315b62ca50ca4;hb=8b405c81769b822dd2d0db28c613da259f68c071;hp=3fb7ea2a8c3fe69f9b1bb12516073f826d31670b;hpb=48ab808f1454463ca2b960d6301fa0f47f21b4ad;p=vchess.git diff --git a/server/sockets.js b/server/sockets.js index 3fb7ea2a..42a8840f 100644 --- a/server/sockets.js +++ b/server/sockets.js @@ -152,15 +152,22 @@ module.exports = function(wss) { case "askfullgame": { const pg = obj.page || page; //required for askidentity and askgame - const tmpIds = Object.keys(clients[pg][obj.target]); - if (obj.target == sid) //targetting myself + // In cas askfullgame to wrong SID for example, would crash: + if (clients[pg] && clients[pg][obj.target]) { - const idx_myTmpid = tmpIds.findIndex(x => x == tmpId); - if (idx_myTmpid >= 0) - tmpIds.splice(idx_myTmpid, 1); + const tmpIds = Object.keys(clients[pg][obj.target]); + if (obj.target == sid) //targetting myself + { + const idx_myTmpid = tmpIds.findIndex(x => x == tmpId); + if (idx_myTmpid >= 0) + tmpIds.splice(idx_myTmpid, 1); + } + const tmpId_idx = Math.floor(Math.random() * tmpIds.length); + send( + clients[pg][obj.target][tmpIds[tmpId_idx]], + {code:obj.code, from:[sid,tmpId,page]} + ); } - const tmpId_idx = Math.floor(Math.random() * tmpIds.length); - send(clients[pg][obj.target][tmpIds[tmpId_idx]], {code:obj.code, from:[sid,tmpId,page]}); break; } @@ -200,7 +207,10 @@ module.exports = function(wss) { case "lastate": { 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}); + // NOTE: if in game we ask identity to opponent still in Hall, + // but leaving Hall, clients[pg] or clients[pg][target] could be ndefined + if (clients[pg] && clients[pg][obj.target[0]]) + send(clients[pg][obj.target[0]][obj.target[1]], {code:obj.code, data:obj.data}); break; } }