X-Git-Url: https://git.auder.net/?p=vchess.git;a=blobdiff_plain;f=server%2Fsockets.js;h=3b16653016a96e3deb40931a490f098e23efbd6c;hp=b547c6a1fe9893b34346aede7e922cd9b1afaa45;hb=c97830ea3ee97c6c408c62dab6c59da46cfd03d5;hpb=80ee5d5a70f17f78900a8a3ae2d803ed1f2f14c9 diff --git a/server/sockets.js b/server/sockets.js index b547c6a1..3b166530 100644 --- a/server/sockets.js +++ b/server/sockets.js @@ -33,14 +33,12 @@ module.exports = function(wss) { }); }; notifyRoom(query["page"], "connect"); //Hall or Game + if (query["page"].indexOf("/game/") >= 0) + notifyRoom("/", "connect"); //notify main hall socket.on("message", objtxt => { let obj = JSON.parse(objtxt); if (!!obj.target && !clients[obj.target]) return; //receiver not connected, nothing we can do - -console.log(obj.code); -console.log(clients); - switch (obj.code) { case "pollclients": @@ -54,8 +52,12 @@ console.log(clients); break; case "pagechange": notifyRoom(clients[sid].page, "disconnect"); + if (clients[sid].page.indexOf("/game/") >= 0) + notifyRoom("/", "disconnect"); clients[sid].page = obj.page; notifyRoom(obj.page, "connect"); + if (obj.page.indexOf("/game/") >= 0) + notifyRoom("/", "connect"); break; case "askidentity": clients[obj.target].sock.send(JSON.stringify( @@ -65,7 +67,7 @@ console.log(clients); clients[obj.target].sock.send(JSON.stringify( {code:"askchallenge",from:sid})); break; - case "askgame": + case "askgames": // Check all clients playing, and send them a "askgame" message Object.keys(clients).forEach(k => { if (k != sid && clients[k].page.indexOf("/game/") >= 0) @@ -74,8 +76,6 @@ console.log(clients); {code:"askgame", from: sid})); } }); - clients[obj.target].sock.send(JSON.stringify( - {code:"askgame",from:sid})); break; case "identity": clients[obj.target].sock.send(JSON.stringify( @@ -110,7 +110,9 @@ console.log(clients); } break; case "newchat": - notifyRoom(query["page"], "newchat", {msg:obj.msg, name:obj.name}); + // WARNING: do not use query["page"], because the page may change + notifyRoom(clients[sid].page, "newchat", + {msg: obj.msg, name: obj.name}); break; // TODO: WebRTC instead in this case (most demanding?) case "newmove": @@ -143,6 +145,8 @@ console.log(clients); const page = clients[sid].page; delete clients[sid]; notifyRoom(page, "disconnect"); + if (page.indexOf("/game/") >= 0) + notifyRoom("/", "disconnect"); //notify main hall }); }); }