| 1 | const url = require('url'); |
| 2 | |
| 3 | // Node version in Ubuntu 16.04 does not know about URL class |
| 4 | function getJsonFromUrl(url) |
| 5 | { |
| 6 | const query = url.substr(2); //starts with "/?" |
| 7 | let result = {}; |
| 8 | query.split("&").forEach((part) => { |
| 9 | const item = part.split("="); |
| 10 | result[item[0]] = decodeURIComponent(item[1]); |
| 11 | }); |
| 12 | return result; |
| 13 | } |
| 14 | |
| 15 | module.exports = function(wss) { |
| 16 | let clients = {}; //associative array sid --> socket |
| 17 | wss.on("connection", (socket, req) => { |
| 18 | const query = getJsonFromUrl(req.url); |
| 19 | const sid = query["sid"]; |
| 20 | // TODO: later, allow duplicate connections (shouldn't be much more complicated) |
| 21 | if (!!clients[sid]) |
| 22 | return socket.send(JSON.stringify({code:"duplicate"})); |
| 23 | clients[sid] = {sock: socket, page: query["page"]}; |
| 24 | const notifyRoom = (page,code,obj={},excluded=[]) => { |
| 25 | Object.keys(clients).forEach(k => { |
| 26 | if (k in excluded) |
| 27 | return; |
| 28 | if (k != sid && clients[k].page == page) |
| 29 | { |
| 30 | clients[k].sock.send(JSON.stringify(Object.assign( |
| 31 | {code:code, from:sid}, obj))); |
| 32 | } |
| 33 | }); |
| 34 | }; |
| 35 | notifyRoom(query["page"], "connect"); //Hall or Game |
| 36 | socket.on("message", objtxt => { |
| 37 | let obj = JSON.parse(objtxt); |
| 38 | if (!!obj.target && !clients[obj.target]) |
| 39 | return; //receiver not connected, nothing we can do |
| 40 | |
| 41 | console.log(obj.code); |
| 42 | console.log(clients); |
| 43 | |
| 44 | switch (obj.code) |
| 45 | { |
| 46 | case "pollclients": |
| 47 | const curPage = clients[sid].page; |
| 48 | socket.send(JSON.stringify({code:"pollclients", |
| 49 | sockIds: Object.keys(clients).filter(k => k != sid && |
| 50 | (clients[k].page == curPage || |
| 51 | // Consider that people playing are in Hall too: |
| 52 | (curPage == "/" && clients[k].page.indexOf("/game/") >= 0)) |
| 53 | )})); |
| 54 | break; |
| 55 | case "pagechange": |
| 56 | notifyRoom(clients[sid].page, "disconnect"); |
| 57 | clients[sid].page = obj.page; |
| 58 | notifyRoom(obj.page, "connect"); |
| 59 | break; |
| 60 | case "askidentity": |
| 61 | clients[obj.target].sock.send(JSON.stringify( |
| 62 | {code:"askidentity",from:sid})); |
| 63 | break; |
| 64 | case "askchallenge": |
| 65 | clients[obj.target].sock.send(JSON.stringify( |
| 66 | {code:"askchallenge",from:sid})); |
| 67 | break; |
| 68 | case "askgame": |
| 69 | // Check all clients playing, and send them a "askgame" message |
| 70 | Object.keys(clients).forEach(k => { |
| 71 | if (k != sid && clients[k].page.indexOf("/game/") >= 0) |
| 72 | { |
| 73 | clients[k].sock.send(JSON.stringify( |
| 74 | {code:"askgame", from: sid})); |
| 75 | } |
| 76 | }); |
| 77 | clients[obj.target].sock.send(JSON.stringify( |
| 78 | {code:"askgame",from:sid})); |
| 79 | break; |
| 80 | case "identity": |
| 81 | clients[obj.target].sock.send(JSON.stringify( |
| 82 | {code:"identity",user:obj.user})); |
| 83 | break; |
| 84 | case "refusechallenge": |
| 85 | clients[obj.target].sock.send(JSON.stringify( |
| 86 | {code:"refusechallenge", cid:obj.cid, from:sid})); |
| 87 | break; |
| 88 | case "deletechallenge": |
| 89 | clients[obj.target].sock.send(JSON.stringify( |
| 90 | {code:"deletechallenge", cid:obj.cid, from:sid})); |
| 91 | break; |
| 92 | case "newgame": |
| 93 | clients[obj.target].sock.send(JSON.stringify( |
| 94 | {code:"newgame", gameInfo:obj.gameInfo, cid:obj.cid})); |
| 95 | break; |
| 96 | case "challenge": |
| 97 | clients[obj.target].sock.send(JSON.stringify( |
| 98 | {code:"challenge", chall:obj.chall, from:sid})); |
| 99 | break; |
| 100 | case "game": |
| 101 | if (!!obj.target) |
| 102 | { |
| 103 | clients[obj.target].sock.send(JSON.stringify( |
| 104 | {code:"game", game:obj.game, from:sid})); |
| 105 | } |
| 106 | else |
| 107 | { |
| 108 | // Notify all room except opponent and me: |
| 109 | notifyRoom("/", "game", {game:obj.game}, [obj.oppsid]); |
| 110 | } |
| 111 | break; |
| 112 | case "newchat": |
| 113 | notifyRoom(query["page"], "newchat", {msg:obj.msg, name:obj.name}); |
| 114 | break; |
| 115 | // TODO: WebRTC instead in this case (most demanding?) |
| 116 | case "newmove": |
| 117 | clients[obj.target].sock.send(JSON.stringify( |
| 118 | {code:"newmove", move:obj.move})); |
| 119 | break; |
| 120 | case "lastate": |
| 121 | clients[obj.target].sock.send(JSON.stringify( |
| 122 | {code:"lastate", state:obj.state})); |
| 123 | break; |
| 124 | case "resign": |
| 125 | clients[obj.target].sock.send(JSON.stringify( |
| 126 | {code:"resign"})); |
| 127 | break; |
| 128 | case "abort": |
| 129 | clients[obj.target].sock.send(JSON.stringify( |
| 130 | {code:"abort",msg:obj.msg})); |
| 131 | break; |
| 132 | case "drawoffer": |
| 133 | clients[obj.target].sock.send(JSON.stringify( |
| 134 | {code:"drawoffer"})); |
| 135 | break; |
| 136 | case "draw": |
| 137 | clients[obj.target].sock.send(JSON.stringify( |
| 138 | {code:"draw"})); |
| 139 | break; |
| 140 | } |
| 141 | }); |
| 142 | socket.on("close", () => { |
| 143 | const page = clients[sid].page; |
| 144 | delete clients[sid]; |
| 145 | notifyRoom(page, "disconnect"); |
| 146 | }); |
| 147 | }); |
| 148 | } |