Fix online indicators between Hall and Game pages
[vchess.git] / server / sockets.js
CommitLineData
a29d9d6b 1const url = require('url');
1d184b4c 2
2807f530 3// Node version in Ubuntu 16.04 does not know about URL class
98db2082
BA
4function getJsonFromUrl(url)
5{
80ee5d5a
BA
6 const query = url.substr(2); //starts with "/?"
7 let result = {};
8 query.split("&").forEach((part) => {
5bd05dba
BA
9 const item = part.split("=");
10 result[item[0]] = decodeURIComponent(item[1]);
11 });
12 return result;
2807f530
BA
13}
14
1d184b4c 15module.exports = function(wss) {
5bd05dba
BA
16 let clients = {}; //associative array sid --> socket
17 wss.on("connection", (socket, req) => {
18 const query = getJsonFromUrl(req.url);
19 const sid = query["sid"];
d4036efe 20 // TODO: later, allow duplicate connections (shouldn't be much more complicated)
5bd05dba
BA
21 if (!!clients[sid])
22 return socket.send(JSON.stringify({code:"duplicate"}));
92a523d1 23 clients[sid] = {sock: socket, page: query["page"]};
c6788ecf 24 const notifyRoom = (page,code,obj={},excluded=[]) => {
92a523d1 25 Object.keys(clients).forEach(k => {
c6788ecf
BA
26 if (k in excluded)
27 return;
92a523d1 28 if (k != sid && clients[k].page == page)
5c8e044f
BA
29 {
30 clients[k].sock.send(JSON.stringify(Object.assign(
c6788ecf 31 {code:code, from:sid}, obj)));
5c8e044f 32 }
92a523d1
BA
33 });
34 };
c6788ecf 35 notifyRoom(query["page"], "connect"); //Hall or Game
bcaa8c00
BA
36 if (query["page"].indexOf("/game/") >= 0)
37 notifyRoom("/", "connect"); //notify main hall
5a3da968 38 socket.on("message", objtxt => {
5bd05dba 39 let obj = JSON.parse(objtxt);
5a3da968 40 if (!!obj.target && !clients[obj.target])
b4d619d1 41 return; //receiver not connected, nothing we can do
c6788ecf
BA
42
43console.log(obj.code);
c6788ecf 44
5bd05dba
BA
45 switch (obj.code)
46 {
81d9ce72 47 case "pollclients":
f41ce580 48 const curPage = clients[sid].page;
81d9ce72 49 socket.send(JSON.stringify({code:"pollclients",
c6788ecf
BA
50 sockIds: Object.keys(clients).filter(k => k != sid &&
51 (clients[k].page == curPage ||
52 // Consider that people playing are in Hall too:
53 (curPage == "/" && clients[k].page.indexOf("/game/") >= 0))
54 )}));
92a523d1
BA
55 break;
56 case "pagechange":
c6788ecf 57 notifyRoom(clients[sid].page, "disconnect");
bcaa8c00
BA
58 if (clients[sid].page.indexOf("/game/") >= 0)
59 notifyRoom("/", "disconnect");
92a523d1 60 clients[sid].page = obj.page;
c6788ecf 61 notifyRoom(obj.page, "connect");
bcaa8c00
BA
62 if (obj.page.indexOf("/game/") >= 0)
63 notifyRoom("/", "connect");
5a3da968
BA
64 break;
65 case "askidentity":
f41ce580
BA
66 clients[obj.target].sock.send(JSON.stringify(
67 {code:"askidentity",from:sid}));
81d9ce72 68 break;
dd75774d 69 case "askchallenge":
f41ce580
BA
70 clients[obj.target].sock.send(JSON.stringify(
71 {code:"askchallenge",from:sid}));
81d9ce72
BA
72 break;
73 case "askgame":
c6788ecf
BA
74 // Check all clients playing, and send them a "askgame" message
75 Object.keys(clients).forEach(k => {
76 if (k != sid && clients[k].page.indexOf("/game/") >= 0)
77 {
78 clients[k].sock.send(JSON.stringify(
79 {code:"askgame", from: sid}));
80 }
81 });
f41ce580
BA
82 clients[obj.target].sock.send(JSON.stringify(
83 {code:"askgame",from:sid}));
5a3da968
BA
84 break;
85 case "identity":
f41ce580
BA
86 clients[obj.target].sock.send(JSON.stringify(
87 {code:"identity",user:obj.user}));
4d64881e 88 break;
5bd05dba 89 case "refusechallenge":
f41ce580
BA
90 clients[obj.target].sock.send(JSON.stringify(
91 {code:"refusechallenge", cid:obj.cid, from:sid}));
5bd05dba 92 break;
98f48579 93 case "deletechallenge":
f41ce580
BA
94 clients[obj.target].sock.send(JSON.stringify(
95 {code:"deletechallenge", cid:obj.cid, from:sid}));
98f48579 96 break;
a6bddfc6 97 case "newgame":
92a523d1 98 clients[obj.target].sock.send(JSON.stringify(
5bd05dba 99 {code:"newgame", gameInfo:obj.gameInfo, cid:obj.cid}));
a6bddfc6 100 break;
42c15a75 101 case "challenge":
92a523d1 102 clients[obj.target].sock.send(JSON.stringify(
42c15a75
BA
103 {code:"challenge", chall:obj.chall, from:sid}));
104 break;
dd75774d 105 case "game":
c6788ecf
BA
106 if (!!obj.target)
107 {
108 clients[obj.target].sock.send(JSON.stringify(
80ee5d5a 109 {code:"game", game:obj.game, from:sid}));
c6788ecf
BA
110 }
111 else
112 {
113 // Notify all room except opponent and me:
80ee5d5a 114 notifyRoom("/", "game", {game:obj.game}, [obj.oppsid]);
c6788ecf 115 }
4d64881e 116 break;
5bd05dba 117 case "newchat":
c6788ecf 118 notifyRoom(query["page"], "newchat", {msg:obj.msg, name:obj.name});
5bd05dba 119 break;
5bd05dba
BA
120 // TODO: WebRTC instead in this case (most demanding?)
121 case "newmove":
f41ce580 122 clients[obj.target].sock.send(JSON.stringify(
c6788ecf 123 {code:"newmove", move:obj.move}));
5bd05dba
BA
124 break;
125 case "lastate":
f41ce580
BA
126 clients[obj.target].sock.send(JSON.stringify(
127 {code:"lastate", state:obj.state}));
5bd05dba 128 break;
5bd05dba 129 case "resign":
f41ce580
BA
130 clients[obj.target].sock.send(JSON.stringify(
131 {code:"resign"}));
5bd05dba 132 break;
b988c726 133 case "abort":
f41ce580
BA
134 clients[obj.target].sock.send(JSON.stringify(
135 {code:"abort",msg:obj.msg}));
b988c726 136 break;
2cc10cdb 137 case "drawoffer":
f41ce580
BA
138 clients[obj.target].sock.send(JSON.stringify(
139 {code:"drawoffer"}));
2cc10cdb
BA
140 break;
141 case "draw":
f41ce580
BA
142 clients[obj.target].sock.send(JSON.stringify(
143 {code:"draw"}));
2cc10cdb 144 break;
5bd05dba
BA
145 }
146 });
147 socket.on("close", () => {
92a523d1 148 const page = clients[sid].page;
5bd05dba 149 delete clients[sid];
92a523d1 150 notifyRoom(page, "disconnect");
bcaa8c00
BA
151 if (page.indexOf("/game/") >= 0)
152 notifyRoom("/", "disconnect"); //notify main hall
5bd05dba
BA
153 });
154 });
1d184b4c 155}