X-Git-Url: https://git.auder.net/?p=vchess.git;a=blobdiff_plain;f=server%2Fsockets.js;h=c2fd552bc697c4fed96e982f375bb78463d44df9;hp=3637c8f47ea4ef495b4ecd59c44f40a0c26ee205;hb=5c8e044f7bab43ca8573fdf631f9b87daeda3ad0;hpb=f21cd6d9c23da37d729f20ea4c08e56b1a7b10a1 diff --git a/server/sockets.js b/server/sockets.js index 3637c8f4..c2fd552b 100644 --- a/server/sockets.js +++ b/server/sockets.js @@ -21,13 +21,16 @@ module.exports = function(wss) { if (!!clients[sid]) return socket.send(JSON.stringify({code:"duplicate"})); clients[sid] = {sock: socket, page: query["page"]}; - const notifyRoom = (page,code) => { + const notifyRoom = (page,code,obj) => { Object.keys(clients).forEach(k => { if (k != sid && clients[k].page == page) - clients[k].sock.send(JSON.stringify({code:code,sid:sid})); + { + clients[k].sock.send(JSON.stringify(Object.assign( + {code:code}, obj))); + } }); }; - notifyRoom(query["page"],"connect"); + notifyRoom(query["page"],"connect",{sid:sid}); socket.on("message", objtxt => { let obj = JSON.parse(objtxt); if (!!obj.target && !clients[obj.target]) @@ -41,9 +44,9 @@ module.exports = function(wss) { k != sid && clients[k].page == curPage)})); break; case "pagechange": - notifyRoom(clients[sid].page, "disconnect"); + notifyRoom(clients[sid].page, "disconnect", {sid:sid}); clients[sid].page = obj.page; - notifyRoom(obj.page, "connect"); + notifyRoom(obj.page, "connect", {sid:sid}); break; case "askidentity": clients[obj.target].sock.send(JSON.stringify( @@ -82,8 +85,8 @@ module.exports = function(wss) { {code:"game", game:obj.game, from:sid})); break; case "newchat": - clients[obj.target].sock.send(JSON.stringify( - {code:"newchat",msg:obj.msg})); + notifyRoom(query["page"], "newchat", + {msg:obj.msg, name:obj.name, sid:sid}) break; // TODO: WebRTC instead in this case (most demanding?) case "newmove":