Commit | Line | Data |
---|---|---|
a29d9d6b | 1 | const url = require('url'); |
1d184b4c | 2 | |
2807f530 | 3 | // Node version in Ubuntu 16.04 does not know about URL class |
98db2082 BA |
4 | function getJsonFromUrl(url) |
5 | { | |
5bd05dba BA |
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; | |
2807f530 BA |
13 | } |
14 | ||
1d184b4c | 15 | module.exports = function(wss) { |
5bd05dba BA |
16 | let clients = {}; //associative array sid --> socket |
17 | wss.on("connection", (socket, req) => { | |
18 | const query = getJsonFromUrl(req.url); | |
19 | const sid = query["sid"]; | |
d4036efe | 20 | // TODO: later, allow duplicate connections (shouldn't be much more complicated) |
5bd05dba BA |
21 | if (!!clients[sid]) |
22 | return socket.send(JSON.stringify({code:"duplicate"})); | |
92a523d1 BA |
23 | clients[sid] = {sock: socket, page: query["page"]}; |
24 | const notifyRoom = (page,code) => { | |
25 | Object.keys(clients).forEach(k => { | |
26 | if (k != sid && clients[k].page == page) | |
27 | clients[k].sock.send(JSON.stringify({code:code,sid:sid})); | |
28 | }); | |
29 | }; | |
30 | notifyRoom(query["page"],"connect"); | |
5a3da968 | 31 | socket.on("message", objtxt => { |
5bd05dba | 32 | let obj = JSON.parse(objtxt); |
5a3da968 | 33 | if (!!obj.target && !clients[obj.target]) |
b4d619d1 | 34 | return; //receiver not connected, nothing we can do |
5bd05dba BA |
35 | switch (obj.code) |
36 | { | |
81d9ce72 | 37 | case "pollclients": |
f41ce580 | 38 | const curPage = clients[sid].page; |
81d9ce72 | 39 | socket.send(JSON.stringify({code:"pollclients", |
92a523d1 | 40 | sockIds: Object.keys(clients).filter(k => |
f41ce580 | 41 | k != sid && clients[k].page == curPage)})); |
92a523d1 BA |
42 | break; |
43 | case "pagechange": | |
44 | notifyRoom(clients[sid].page, "disconnect"); | |
45 | clients[sid].page = obj.page; | |
46 | notifyRoom(obj.page, "connect"); | |
5a3da968 BA |
47 | break; |
48 | case "askidentity": | |
f41ce580 BA |
49 | clients[obj.target].sock.send(JSON.stringify( |
50 | {code:"askidentity",from:sid})); | |
81d9ce72 | 51 | break; |
dd75774d | 52 | case "askchallenge": |
f41ce580 BA |
53 | clients[obj.target].sock.send(JSON.stringify( |
54 | {code:"askchallenge",from:sid})); | |
81d9ce72 BA |
55 | break; |
56 | case "askgame": | |
f41ce580 BA |
57 | clients[obj.target].sock.send(JSON.stringify( |
58 | {code:"askgame",from:sid})); | |
5a3da968 BA |
59 | break; |
60 | case "identity": | |
f41ce580 BA |
61 | clients[obj.target].sock.send(JSON.stringify( |
62 | {code:"identity",user:obj.user})); | |
4d64881e | 63 | break; |
5bd05dba | 64 | case "refusechallenge": |
f41ce580 BA |
65 | clients[obj.target].sock.send(JSON.stringify( |
66 | {code:"refusechallenge", cid:obj.cid, from:sid})); | |
5bd05dba | 67 | break; |
98f48579 | 68 | case "deletechallenge": |
f41ce580 BA |
69 | clients[obj.target].sock.send(JSON.stringify( |
70 | {code:"deletechallenge", cid:obj.cid, from:sid})); | |
98f48579 | 71 | break; |
a6bddfc6 | 72 | case "newgame": |
92a523d1 | 73 | clients[obj.target].sock.send(JSON.stringify( |
5bd05dba | 74 | {code:"newgame", gameInfo:obj.gameInfo, cid:obj.cid})); |
a6bddfc6 | 75 | break; |
42c15a75 | 76 | case "challenge": |
92a523d1 | 77 | clients[obj.target].sock.send(JSON.stringify( |
42c15a75 BA |
78 | {code:"challenge", chall:obj.chall, from:sid})); |
79 | break; | |
dd75774d | 80 | case "game": |
92a523d1 | 81 | clients[obj.target].sock.send(JSON.stringify( |
42c15a75 | 82 | {code:"game", game:obj.game, from:sid})); |
4d64881e | 83 | break; |
5bd05dba | 84 | case "newchat": |
f41ce580 BA |
85 | clients[obj.target].sock.send(JSON.stringify( |
86 | {code:"newchat",msg:obj.msg})); | |
5bd05dba | 87 | break; |
5bd05dba BA |
88 | // TODO: WebRTC instead in this case (most demanding?) |
89 | case "newmove": | |
f41ce580 BA |
90 | clients[obj.target].sock.send(JSON.stringify( |
91 | {code:"newmove",move:obj.move})); | |
5bd05dba BA |
92 | break; |
93 | case "lastate": | |
f41ce580 BA |
94 | clients[obj.target].sock.send(JSON.stringify( |
95 | {code:"lastate", state:obj.state})); | |
5bd05dba | 96 | break; |
5bd05dba | 97 | case "resign": |
f41ce580 BA |
98 | clients[obj.target].sock.send(JSON.stringify( |
99 | {code:"resign"})); | |
5bd05dba | 100 | break; |
b988c726 | 101 | case "abort": |
f41ce580 BA |
102 | clients[obj.target].sock.send(JSON.stringify( |
103 | {code:"abort",msg:obj.msg})); | |
b988c726 | 104 | break; |
2cc10cdb | 105 | case "drawoffer": |
f41ce580 BA |
106 | clients[obj.target].sock.send(JSON.stringify( |
107 | {code:"drawoffer"})); | |
2cc10cdb BA |
108 | break; |
109 | case "draw": | |
f41ce580 BA |
110 | clients[obj.target].sock.send(JSON.stringify( |
111 | {code:"draw"})); | |
2cc10cdb | 112 | break; |
5bd05dba BA |
113 | } |
114 | }); | |
115 | socket.on("close", () => { | |
92a523d1 | 116 | const page = clients[sid].page; |
5bd05dba | 117 | delete clients[sid]; |
92a523d1 | 118 | notifyRoom(page, "disconnect"); |
5bd05dba BA |
119 | }); |
120 | }); | |
1d184b4c | 121 | } |