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