d296efa1a073a47b1818b32bbbbb074263ee134d
[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 // Wait for "connect" message to notify connection to the room,
35 // because if game loading is slow the message listener might
36 // not be ready too early.
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
42 // TODO: debug
43 console.log(obj.code + " " + clients[sid].page);
44
45 switch (obj.code)
46 {
47 case "connect":
48 notifyRoom(query["page"], "connect"); //Hall or Game
49 if (query["page"].indexOf("/game/") >= 0)
50 notifyRoom("/", "gconnect"); //notify main hall
51 break;
52 case "pollclients":
53 {
54 const curPage = clients[sid].page;
55 socket.send(JSON.stringify({code:"pollclients",
56 sockIds: Object.keys(clients).filter(k =>
57 k != sid && clients[k].page == curPage
58 )}));
59 break;
60 }
61 case "pollgamers":
62 {
63 const curPage = clients[sid].page;
64 socket.send(JSON.stringify({code:"pollgamers",
65 sockIds: Object.keys(clients).filter(k =>
66 k != sid && clients[k].page.indexOf("/game/") >= 0
67 )}));
68 break;
69 }
70 case "pagechange":
71 notifyRoom(clients[sid].page, "disconnect");
72 if (clients[sid].page.indexOf("/game/") >= 0)
73 notifyRoom("/", "gdisconnect");
74 clients[sid].page = obj.page;
75 notifyRoom(obj.page, "connect");
76 if (obj.page.indexOf("/game/") >= 0)
77 notifyRoom("/", "gconnect");
78 break;
79 case "askidentity":
80 clients[obj.target].sock.send(JSON.stringify(
81 {code:"askidentity",from:sid}));
82 break;
83 case "askchallenge":
84 clients[obj.target].sock.send(JSON.stringify(
85 {code:"askchallenge",from:sid}));
86 break;
87 case "askgames":
88 {
89 // Check all clients playing, and send them a "askgame" message
90 let gameSids = {}; //game ID --> [sid1, sid2]
91 const regexpGid = /\/[a-zA-Z0-9]+$/;
92 Object.keys(clients).forEach(k => {
93 if (k != sid && clients[k].page.indexOf("/game/") >= 0)
94 {
95 const gid = clients[k].page.match(regexpGid)[0];
96 if (!gameSids[gid])
97 gameSids[gid] = [k];
98 else
99 gameSids[gid].push(k);
100 }
101 });
102 // Request only one client out of 2 (TODO: this is a bit heavy)
103 // Alt: ask game to all, and filter later?
104 Object.keys(gameSids).forEach(gid => {
105 const L = gameSids[gid].length;
106 const idx = L > 1
107 ? Math.floor(Math.random() * Math.floor(L))
108 : 0;
109 const rid = gameSids[gid][idx];
110 clients[rid].sock.send(JSON.stringify(
111 {code:"askgame", from: sid}));
112 });
113 break;
114 }
115 case "askfullgame":
116 clients[obj.target].sock.send(JSON.stringify(
117 {code:"askfullgame", from:sid}));
118 break;
119 case "fullgame":
120 clients[obj.target].sock.send(JSON.stringify(
121 {code:"fullgame", game:obj.game}));
122 break;
123 case "identity":
124 clients[obj.target].sock.send(JSON.stringify(
125 {code:"identity",user:obj.user}));
126 break;
127 case "refusechallenge":
128 clients[obj.target].sock.send(JSON.stringify(
129 {code:"refusechallenge", cid:obj.cid, from:sid}));
130 break;
131 case "deletechallenge":
132 clients[obj.target].sock.send(JSON.stringify(
133 {code:"deletechallenge", cid:obj.cid, from:sid}));
134 break;
135 case "newgame":
136 clients[obj.target].sock.send(JSON.stringify(
137 {code:"newgame", gameInfo:obj.gameInfo, cid:obj.cid}));
138 break;
139 case "challenge":
140 clients[obj.target].sock.send(JSON.stringify(
141 {code:"challenge", chall:obj.chall, from:sid}));
142 break;
143 case "game":
144 if (!!obj.target)
145 {
146 clients[obj.target].sock.send(JSON.stringify(
147 {code:"game", game:obj.game, from:sid}));
148 }
149 else
150 {
151 // Notify all room except opponent and me:
152 notifyRoom("/", "game", {game:obj.game}, [obj.oppsid]);
153 }
154 break;
155 case "newchat":
156 notifyRoom(clients[sid].page, "newchat", {chat:obj.chat});
157 break;
158 // TODO: WebRTC instead in this case (most demanding?)
159 // --> At least do a "notifyRoom"
160 case "newmove":
161 clients[obj.target].sock.send(JSON.stringify(
162 {code:"newmove", move:obj.move}));
163 break;
164 case "lastate":
165 clients[obj.target].sock.send(JSON.stringify(
166 {code:"lastate", state:obj.state}));
167 break;
168 case "resign":
169 clients[obj.target].sock.send(JSON.stringify(
170 {code:"resign", side:obj.side}));
171 break;
172 case "abort":
173 clients[obj.target].sock.send(JSON.stringify(
174 {code:"abort"}));
175 break;
176 case "drawoffer":
177 clients[obj.target].sock.send(JSON.stringify(
178 {code:"drawoffer"}));
179 break;
180 case "draw":
181 clients[obj.target].sock.send(JSON.stringify(
182 {code:"draw", message:obj.message}));
183 break;
184 }
185 });
186 socket.on("close", () => {
187 const page = clients[sid].page;
188 delete clients[sid];
189 notifyRoom(page, "disconnect");
190 if (page.indexOf("/game/") >= 0)
191 notifyRoom("/", "gdisconnect"); //notify main hall
192 });
193 });
194 }