X-Git-Url: https://git.auder.net/?p=vchess.git;a=blobdiff_plain;f=server%2Fsockets.js;h=d296efa1a073a47b1818b32bbbbb074263ee134d;hp=fae711066e912709883aa853e05d162c91a351b6;hb=ac8f441c6441d827b43aabeb812cb4c79e9ee96b;hpb=3837d4f7885a3c3cdb468da2f3fa3fa1e5a1415a diff --git a/server/sockets.js b/server/sockets.js index fae71106..d296efa1 100644 --- a/server/sockets.js +++ b/server/sockets.js @@ -31,32 +31,50 @@ module.exports = function(wss) { } }); }; - notifyRoom(query["page"], "connect"); //Hall or Game - if (query["page"].indexOf("/game/") >= 0) - notifyRoom("/", "connect"); //notify main hall + // Wait for "connect" message to notify connection to the room, + // because if game loading is slow the message listener might + // not be ready too early. socket.on("message", objtxt => { let obj = JSON.parse(objtxt); if (!!obj.target && !clients[obj.target]) return; //receiver not connected, nothing we can do + +// TODO: debug +console.log(obj.code + " " + clients[sid].page); + switch (obj.code) { + case "connect": + notifyRoom(query["page"], "connect"); //Hall or Game + if (query["page"].indexOf("/game/") >= 0) + notifyRoom("/", "gconnect"); //notify main hall + break; case "pollclients": + { const curPage = clients[sid].page; socket.send(JSON.stringify({code:"pollclients", - sockIds: Object.keys(clients).filter(k => k != sid && - (clients[k].page == curPage || - // Consider that people playing are in Hall too: - (curPage == "/" && clients[k].page.indexOf("/game/") >= 0)) + sockIds: Object.keys(clients).filter(k => + k != sid && clients[k].page == curPage + )})); + break; + } + case "pollgamers": + { + const curPage = clients[sid].page; + socket.send(JSON.stringify({code:"pollgamers", + sockIds: Object.keys(clients).filter(k => + k != sid && clients[k].page.indexOf("/game/") >= 0 )})); break; + } case "pagechange": notifyRoom(clients[sid].page, "disconnect"); if (clients[sid].page.indexOf("/game/") >= 0) - notifyRoom("/", "disconnect"); + notifyRoom("/", "gdisconnect"); clients[sid].page = obj.page; notifyRoom(obj.page, "connect"); if (obj.page.indexOf("/game/") >= 0) - notifyRoom("/", "connect"); + notifyRoom("/", "gconnect"); break; case "askidentity": clients[obj.target].sock.send(JSON.stringify( @@ -135,11 +153,10 @@ module.exports = function(wss) { } break; case "newchat": - // WARNING: do not use query["page"], because the page may change - notifyRoom(clients[sid].page, "newchat", - {msg: obj.msg, name: obj.name}); + notifyRoom(clients[sid].page, "newchat", {chat:obj.chat}); break; // TODO: WebRTC instead in this case (most demanding?) + // --> At least do a "notifyRoom" case "newmove": clients[obj.target].sock.send(JSON.stringify( {code:"newmove", move:obj.move})); @@ -162,7 +179,7 @@ module.exports = function(wss) { break; case "draw": clients[obj.target].sock.send(JSON.stringify( - {code:"draw"})); + {code:"draw", message:obj.message})); break; } }); @@ -171,7 +188,7 @@ module.exports = function(wss) { delete clients[sid]; notifyRoom(page, "disconnect"); if (page.indexOf("/game/") >= 0) - notifyRoom("/", "disconnect"); //notify main hall + notifyRoom("/", "gdisconnect"); //notify main hall }); }); }