Merge branch 'master' of auder.net:vchess
[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 // NOTE: url is already transformed, without ?xxx=yyy... parts
5 function getJsonFromUrl(url)
6 {
7 const query = url.substr(2); //starts with "/?"
8 let result = {};
9 query.split("&").forEach((part) => {
10 const item = part.split("=");
11 result[item[0]] = decodeURIComponent(item[1]);
12 });
13 return result;
14 }
15
16 module.exports = function(wss) {
17 let clients = {}; //associative array sid --> socket
18 wss.on("connection", (socket, req) => {
19 const query = getJsonFromUrl(req.url);
20 if (query["page"] != "/" && query["page"].indexOf("/game/") < 0)
21 return; //other tabs don't need to be connected
22 const sid = query["sid"];
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 const messageListener = (objtxt) => {
35 let obj = JSON.parse(objtxt);
36 if (!!obj.target && !clients[obj.target])
37 return; //receiver not connected, nothing we can do
38 switch (obj.code)
39 {
40 case "duplicate":
41 // Turn off message listening, and send disconnect if needed:
42 socket.removeListener("message", messageListener);
43 socket.removeListener("close", closeListener);
44 // From obj.page to clients[sid].page (TODO: unclear)
45 if (clients[sid].page != obj.page)
46 {
47 notifyRoom(obj.page, "disconnect");
48 if (obj.page.indexOf("/game/") >= 0)
49 notifyRoom("/", "gdisconnect");
50 }
51 break;
52 // Wait for "connect" message to notify connection to the room,
53 // because if game loading is slow the message listener might
54 // not be ready too early.
55 case "connect":
56 {
57 const curPage = clients[sid].page;
58 notifyRoom(curPage, "connect"); //Hall or Game
59 if (curPage.indexOf("/game/") >= 0)
60 notifyRoom("/", "gconnect"); //notify main hall
61 break;
62 }
63 case "pollclients":
64 {
65 const curPage = clients[sid].page;
66 socket.send(JSON.stringify({code:"pollclients",
67 sockIds: Object.keys(clients).filter(k =>
68 k != sid && clients[k].page == curPage
69 )}));
70 break;
71 }
72 case "pollgamers":
73 socket.send(JSON.stringify({code:"pollgamers",
74 sockIds: Object.keys(clients).filter(k =>
75 k != sid && clients[k].page.indexOf("/game/") >= 0
76 )}));
77 break;
78 case "pagechange":
79 // page change clients[sid].page --> obj.page
80 // TODO: some offline rooms don't need to receive disconnect event
81 notifyRoom(clients[sid].page, "disconnect");
82 if (clients[sid].page.indexOf("/game/") >= 0)
83 notifyRoom("/", "gdisconnect");
84 clients[sid].page = obj.page;
85 // No need to notify connection: it's self-sent in .vue file
86 //notifyRoom(obj.page, "connect");
87 if (obj.page.indexOf("/game/") >= 0)
88 notifyRoom("/", "gconnect");
89 break;
90 case "askidentity":
91 clients[obj.target].sock.send(JSON.stringify(
92 {code:"askidentity",from:sid}));
93 break;
94 case "asklastate":
95 clients[obj.target].sock.send(JSON.stringify(
96 {code:"asklastate",from:sid}));
97 break;
98 case "askchallenge":
99 clients[obj.target].sock.send(JSON.stringify(
100 {code:"askchallenge",from:sid}));
101 break;
102 case "askgames":
103 {
104 // Check all clients playing, and send them a "askgame" message
105 let gameSids = {}; //game ID --> [sid1, sid2]
106 const regexpGid = /\/[a-zA-Z0-9]+$/;
107 Object.keys(clients).forEach(k => {
108 if (k != sid && clients[k].page.indexOf("/game/") >= 0)
109 {
110 const gid = clients[k].page.match(regexpGid)[0];
111 if (!gameSids[gid])
112 gameSids[gid] = [k];
113 else
114 gameSids[gid].push(k);
115 }
116 });
117 // Request only one client out of 2 (TODO: this is a bit heavy)
118 // Alt: ask game to all, and filter later?
119 Object.keys(gameSids).forEach(gid => {
120 const L = gameSids[gid].length;
121 const idx = L > 1
122 ? Math.floor(Math.random() * Math.floor(L))
123 : 0;
124 const rid = gameSids[gid][idx];
125 clients[rid].sock.send(JSON.stringify(
126 {code:"askgame", from: sid}));
127 });
128 break;
129 }
130 case "askgame":
131 clients[obj.target].sock.send(JSON.stringify(
132 {code:"askgame", from:sid}));
133 break;
134 case "askfullgame":
135 clients[obj.target].sock.send(JSON.stringify(
136 {code:"askfullgame", from:sid}));
137 break;
138 case "fullgame":
139 clients[obj.target].sock.send(JSON.stringify(
140 {code:"fullgame", game:obj.game}));
141 break;
142 case "identity":
143 clients[obj.target].sock.send(JSON.stringify(
144 {code:"identity",user:obj.user}));
145 break;
146 case "refusechallenge":
147 clients[obj.target].sock.send(JSON.stringify(
148 {code:"refusechallenge", cid:obj.cid, from:sid}));
149 break;
150 case "deletechallenge":
151 clients[obj.target].sock.send(JSON.stringify(
152 {code:"deletechallenge", cid:obj.cid, from:sid}));
153 break;
154 case "newgame":
155 clients[obj.target].sock.send(JSON.stringify(
156 {code:"newgame", gameInfo:obj.gameInfo, cid:obj.cid}));
157 break;
158 case "challenge":
159 clients[obj.target].sock.send(JSON.stringify(
160 {code:"challenge", chall:obj.chall, from:sid}));
161 break;
162 case "game":
163 if (!!obj.target)
164 {
165 clients[obj.target].sock.send(JSON.stringify(
166 {code:"game", game:obj.game, from:sid}));
167 }
168 else
169 {
170 // Notify all room except opponent and me:
171 notifyRoom("/", "game", {game:obj.game}, [obj.oppsid]);
172 }
173 break;
174 case "newchat":
175 notifyRoom(clients[sid].page, "newchat", {chat:obj.chat});
176 break;
177 // TODO: WebRTC instead in this case (most demanding?)
178 // --> Or else: at least do a "notifyRoom" (also for draw, resign...)
179 case "newmove":
180 clients[obj.target].sock.send(JSON.stringify(
181 {code:"newmove", move:obj.move}));
182 break;
183 case "lastate":
184 clients[obj.target].sock.send(JSON.stringify(
185 {code:"lastate", state:obj.state}));
186 break;
187 case "resign":
188 clients[obj.target].sock.send(JSON.stringify(
189 {code:"resign", side:obj.side}));
190 break;
191 case "abort":
192 clients[obj.target].sock.send(JSON.stringify(
193 {code:"abort"}));
194 break;
195 case "drawoffer":
196 clients[obj.target].sock.send(JSON.stringify(
197 {code:"drawoffer"}));
198 break;
199 case "draw":
200 clients[obj.target].sock.send(JSON.stringify(
201 {code:"draw", message:obj.message}));
202 break;
203 }
204 };
205 const closeListener = () => {
206 const page = clients[sid].page;
207 delete clients[sid];
208 notifyRoom(page, "disconnect");
209 if (page.indexOf("/game/") >= 0)
210 notifyRoom("/", "gdisconnect"); //notify main hall
211 };
212 if (!!clients[sid])
213 {
214 // Turn off old sock through current client:
215 clients[sid].sock.send(JSON.stringify({code:"duplicate"}));
216 }
217 // Potentially replace current connection:
218 clients[sid] = {sock: socket, page: query["page"]};
219 socket.on("message", messageListener);
220 socket.on("close", closeListener);
221 });
222 }