X-Git-Url: https://git.auder.net/?p=vchess.git;a=blobdiff_plain;f=server%2Fsockets.js;h=ef2f07b4d1e57b146c58c140e6a8366f3a3db1a7;hp=ed7275ff54250430091282e59c85ce2bedcae05e;hb=5fd5fb22de02ed6c585e86a1c3dda50ff6071d2f;hpb=ab6f48ea4d9c549830f549f077c597f57ea4a57d diff --git a/server/sockets.js b/server/sockets.js index ed7275ff..ef2f07b4 100644 --- a/server/sockets.js +++ b/server/sockets.js @@ -17,7 +17,6 @@ module.exports = function(wss) { wss.on("connection", (socket, req) => { const query = getJsonFromUrl(req.url); const sid = query["sid"]; - // TODO: later, allow duplicate connections (shouldn't be much more complicated) if (!!clients[sid]) return socket.send(JSON.stringify({code:"duplicate"})); clients[sid] = {sock: socket, page: query["page"]}; @@ -32,15 +31,20 @@ 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 switch (obj.code) { + case "connect": + notifyRoom(query["page"], "connect"); //Hall or Game + if (query["page"].indexOf("/game/") >= 0) + notifyRoom("/", "connect"); //notify main hall + break; case "pollclients": const curPage = clients[sid].page; socket.send(JSON.stringify({code:"pollclients", @@ -151,11 +155,11 @@ module.exports = function(wss) { break; case "resign": clients[obj.target].sock.send(JSON.stringify( - {code:"resign"})); + {code:"resign", side:obj.side})); break; case "abort": clients[obj.target].sock.send(JSON.stringify( - {code:"abort",msg:obj.msg})); + {code:"abort"})); break; case "drawoffer": clients[obj.target].sock.send(JSON.stringify( @@ -163,7 +167,7 @@ module.exports = function(wss) { break; case "draw": clients[obj.target].sock.send(JSON.stringify( - {code:"draw"})); + {code:"draw", message:obj.message})); break; } });