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