e4aa4493c15bad223613509d12a4bbf1de37127b
[vchess.git] / server / sockets.js
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 if (!!clients[sid])
21 return socket.send(JSON.stringify({code:"duplicate"}));
22 clients[sid] = {sock: socket, page: query["page"]};
23 const notifyRoom = (page,code,obj={},excluded=[]) => {
24 Object.keys(clients).forEach(k => {
25 if (k in excluded)
26 return;
27 if (k != sid && clients[k].page == page)
28 {
29 clients[k].sock.send(JSON.stringify(Object.assign(
30 {code:code, from:sid}, obj)));
31 }
32 });
33 };
34 notifyRoom(query["page"], "connect"); //Hall or Game
35 if (query["page"].indexOf("/game/") >= 0)
36 notifyRoom("/", "connect"); //notify main hall
37 socket.on("message", objtxt => {
38 let obj = JSON.parse(objtxt);
39 if (!!obj.target && !clients[obj.target])
40 return; //receiver not connected, nothing we can do
41 switch (obj.code)
42 {
43 case "pollclients":
44 const curPage = clients[sid].page;
45 socket.send(JSON.stringify({code:"pollclients",
46 sockIds: Object.keys(clients).filter(k => k != sid &&
47 (clients[k].page == curPage ||
48 // Consider that people playing are in Hall too:
49 (curPage == "/" && clients[k].page.indexOf("/game/") >= 0))
50 )}));
51 break;
52 case "pagechange":
53 notifyRoom(clients[sid].page, "disconnect");
54 if (clients[sid].page.indexOf("/game/") >= 0)
55 notifyRoom("/", "disconnect");
56 clients[sid].page = obj.page;
57 notifyRoom(obj.page, "connect");
58 if (obj.page.indexOf("/game/") >= 0)
59 notifyRoom("/", "connect");
60 break;
61 case "askidentity":
62 clients[obj.target].sock.send(JSON.stringify(
63 {code:"askidentity",from:sid}));
64 break;
65 case "askchallenge":
66 clients[obj.target].sock.send(JSON.stringify(
67 {code:"askchallenge",from:sid}));
68 break;
69 case "askgames":
70 {
71 // Check all clients playing, and send them a "askgame" message
72 let gameSids = {}; //game ID --> [sid1, sid2]
73 const regexpGid = /\/[a-zA-Z0-9]+$/;
74 Object.keys(clients).forEach(k => {
75 if (k != sid && clients[k].page.indexOf("/game/") >= 0)
76 {
77 const gid = clients[k].page.match(regexpGid)[0];
78 if (!gameSids[gid])
79 gameSids[gid] = [k];
80 else
81 gameSids[gid].push(k);
82 }
83 });
84 // Request only one client out of 2 (TODO: this is a bit heavy)
85 // Alt: ask game to all, and filter later?
86 Object.keys(gameSids).forEach(gid => {
87 const L = gameSids[gid].length;
88 const idx = L > 1
89 ? Math.floor(Math.random() * Math.floor(L))
90 : 0;
91 const rid = gameSids[gid][idx];
92 clients[rid].sock.send(JSON.stringify(
93 {code:"askgame", from: sid}));
94 });
95 break;
96 }
97 case "askfullgame":
98 clients[obj.target].sock.send(JSON.stringify(
99 {code:"askfullgame", from:sid}));
100 break;
101 case "fullgame":
102 clients[obj.target].sock.send(JSON.stringify(
103 {code:"fullgame", game:obj.game}));
104 break;
105 case "identity":
106 clients[obj.target].sock.send(JSON.stringify(
107 {code:"identity",user:obj.user}));
108 break;
109 case "refusechallenge":
110 clients[obj.target].sock.send(JSON.stringify(
111 {code:"refusechallenge", cid:obj.cid, from:sid}));
112 break;
113 case "deletechallenge":
114 clients[obj.target].sock.send(JSON.stringify(
115 {code:"deletechallenge", cid:obj.cid, from:sid}));
116 break;
117 case "newgame":
118 clients[obj.target].sock.send(JSON.stringify(
119 {code:"newgame", gameInfo:obj.gameInfo, cid:obj.cid}));
120 break;
121 case "challenge":
122 clients[obj.target].sock.send(JSON.stringify(
123 {code:"challenge", chall:obj.chall, from:sid}));
124 break;
125 case "game":
126 if (!!obj.target)
127 {
128 clients[obj.target].sock.send(JSON.stringify(
129 {code:"game", game:obj.game, from:sid}));
130 }
131 else
132 {
133 // Notify all room except opponent and me:
134 notifyRoom("/", "game", {game:obj.game}, [obj.oppsid]);
135 }
136 break;
137 case "newchat":
138 // WARNING: do not use query["page"], because the page may change
139 notifyRoom(clients[sid].page, "newchat",
140 {msg: obj.msg, name: obj.name});
141 break;
142 // TODO: WebRTC instead in this case (most demanding?)
143 case "newmove":
144 clients[obj.target].sock.send(JSON.stringify(
145 {code:"newmove", move:obj.move}));
146 break;
147 case "lastate":
148 clients[obj.target].sock.send(JSON.stringify(
149 {code:"lastate", state:obj.state}));
150 break;
151 case "resign":
152 clients[obj.target].sock.send(JSON.stringify(
153 {code:"resign"}));
154 break;
155 case "abort":
156 clients[obj.target].sock.send(JSON.stringify(
157 {code:"abort",msg:obj.msg}));
158 break;
159 case "drawoffer":
160 clients[obj.target].sock.send(JSON.stringify(
161 {code:"drawoffer"}));
162 break;
163 case "draw":
164 clients[obj.target].sock.send(JSON.stringify(
165 {code:"draw"}));
166 break;
167 }
168 });
169 socket.on("close", () => {
170 const page = clients[sid].page;
171 delete clients[sid];
172 notifyRoom(page, "disconnect");
173 if (page.indexOf("/game/") >= 0)
174 notifyRoom("/", "disconnect"); //notify main hall
175 });
176 });
177 }