X-Git-Url: https://git.auder.net/?p=vchess.git;a=blobdiff_plain;f=server%2Fsockets.js;h=614696f327fe9bd4d7e181177fb22327a45b5239;hp=3854ca2f533ccc764d90bd6a25e17e0f52af66f7;hb=f9c36b2da005b596ad656f4b6cc4e09ef3c656f1;hpb=8d1fcc37a933554e13bc996b5611f8307a4701e8 diff --git a/server/sockets.js b/server/sockets.js index 3854ca2f..614696f3 100644 --- a/server/sockets.js +++ b/server/sockets.js @@ -14,7 +14,7 @@ function getJsonFromUrl(url) { // Helper to safe-send some message through a (web-)socket: function send(socket, message) { - if (socket && socket.readyState == 1) + if (!!socket && socket.readyState == 1) socket.send(JSON.stringify(message)); } @@ -145,7 +145,7 @@ module.exports = function(wss) { case "askfullgame": { const pg = obj.page || page; //required for askidentity and askgame // In cas askfullgame to wrong SID for example, would crash: - if (clients[pg] && clients[pg][obj.target]) { + if (!!clients[pg] && !!clients[pg][obj.target]) { const tmpIds = Object.keys(clients[pg][obj.target]); if (obj.target == sid) { // Targetting myself @@ -183,7 +183,30 @@ module.exports = function(wss) { case "abort": case "drawoffer": case "draw": - notifyRoom(page, obj.code, {data: obj.data}); + // TODO: if newmove, change "from" field to fully specified sid + tmpId + // ==> allow "gotmove" messages to be fully targetted + // Special case re-send newmove only to opponent: + if (!!obj.target) { + Object.keys(clients[page][obj.target]).forEach(x => { + send( + clients[page][obj.target][x], + {code: "newmove", data: obj.data} + ); + }); + } + else notifyRoom(page, obj.code, {data: obj.data}); + break; + + case "gotmove": + // TODO: should fully specify the target and be included in the last case below + if (!!clients[page][obj.target]) { + Object.keys(clients[page][obj.target]).forEach(x => { + send( + clients[pg][obj.target][x], + {code: "gotmove", data: obj.data} + ); + }); + } break; case "result": @@ -222,8 +245,8 @@ module.exports = function(wss) { { const pg = obj.target[2] || page; //required for identity and game // 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]]) + // but leaving Hall, clients[pg] or clients[pg][target] could be undefined + if (!!clients[pg] && !!clients[pg][obj.target[0]]) send(clients[pg][obj.target[0]][obj.target[1]], {code:obj.code, data:obj.data}); break; }