Fix chat issues when launching a game
[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
5bd05dba
BA
42 switch (obj.code)
43 {
81d9ce72 44 case "pollclients":
f41ce580 45 const curPage = clients[sid].page;
81d9ce72 46 socket.send(JSON.stringify({code:"pollclients",
c6788ecf
BA
47 sockIds: Object.keys(clients).filter(k => k != sid &&
48 (clients[k].page == curPage ||
49 // Consider that people playing are in Hall too:
50 (curPage == "/" && clients[k].page.indexOf("/game/") >= 0))
51 )}));
92a523d1
BA
52 break;
53 case "pagechange":
c6788ecf 54 notifyRoom(clients[sid].page, "disconnect");
bcaa8c00
BA
55 if (clients[sid].page.indexOf("/game/") >= 0)
56 notifyRoom("/", "disconnect");
92a523d1 57 clients[sid].page = obj.page;
c6788ecf 58 notifyRoom(obj.page, "connect");
bcaa8c00
BA
59 if (obj.page.indexOf("/game/") >= 0)
60 notifyRoom("/", "connect");
5a3da968
BA
61 break;
62 case "askidentity":
f41ce580
BA
63 clients[obj.target].sock.send(JSON.stringify(
64 {code:"askidentity",from:sid}));
81d9ce72 65 break;
dd75774d 66 case "askchallenge":
f41ce580
BA
67 clients[obj.target].sock.send(JSON.stringify(
68 {code:"askchallenge",from:sid}));
81d9ce72 69 break;
cd0d7743 70 case "askgames":
c6788ecf
BA
71 // Check all clients playing, and send them a "askgame" message
72 Object.keys(clients).forEach(k => {
73 if (k != sid && clients[k].page.indexOf("/game/") >= 0)
74 {
75 clients[k].sock.send(JSON.stringify(
76 {code:"askgame", from: sid}));
77 }
78 });
5a3da968
BA
79 break;
80 case "identity":
f41ce580
BA
81 clients[obj.target].sock.send(JSON.stringify(
82 {code:"identity",user:obj.user}));
4d64881e 83 break;
5bd05dba 84 case "refusechallenge":
f41ce580
BA
85 clients[obj.target].sock.send(JSON.stringify(
86 {code:"refusechallenge", cid:obj.cid, from:sid}));
5bd05dba 87 break;
98f48579 88 case "deletechallenge":
f41ce580
BA
89 clients[obj.target].sock.send(JSON.stringify(
90 {code:"deletechallenge", cid:obj.cid, from:sid}));
98f48579 91 break;
a6bddfc6 92 case "newgame":
92a523d1 93 clients[obj.target].sock.send(JSON.stringify(
5bd05dba 94 {code:"newgame", gameInfo:obj.gameInfo, cid:obj.cid}));
a6bddfc6 95 break;
42c15a75 96 case "challenge":
92a523d1 97 clients[obj.target].sock.send(JSON.stringify(
42c15a75
BA
98 {code:"challenge", chall:obj.chall, from:sid}));
99 break;
dd75774d 100 case "game":
c6788ecf
BA
101 if (!!obj.target)
102 {
103 clients[obj.target].sock.send(JSON.stringify(
80ee5d5a 104 {code:"game", game:obj.game, from:sid}));
c6788ecf
BA
105 }
106 else
107 {
108 // Notify all room except opponent and me:
80ee5d5a 109 notifyRoom("/", "game", {game:obj.game}, [obj.oppsid]);
c6788ecf 110 }
4d64881e 111 break;
5bd05dba 112 case "newchat":
c97830ea
BA
113 // WARNING: do not use query["page"], because the page may change
114 notifyRoom(clients[sid].page, "newchat",
115 {msg: obj.msg, name: obj.name});
5bd05dba 116 break;
5bd05dba
BA
117 // TODO: WebRTC instead in this case (most demanding?)
118 case "newmove":
f41ce580 119 clients[obj.target].sock.send(JSON.stringify(
c6788ecf 120 {code:"newmove", move:obj.move}));
5bd05dba
BA
121 break;
122 case "lastate":
f41ce580
BA
123 clients[obj.target].sock.send(JSON.stringify(
124 {code:"lastate", state:obj.state}));
5bd05dba 125 break;
5bd05dba 126 case "resign":
f41ce580
BA
127 clients[obj.target].sock.send(JSON.stringify(
128 {code:"resign"}));
5bd05dba 129 break;
b988c726 130 case "abort":
f41ce580
BA
131 clients[obj.target].sock.send(JSON.stringify(
132 {code:"abort",msg:obj.msg}));
b988c726 133 break;
2cc10cdb 134 case "drawoffer":
f41ce580
BA
135 clients[obj.target].sock.send(JSON.stringify(
136 {code:"drawoffer"}));
2cc10cdb
BA
137 break;
138 case "draw":
f41ce580
BA
139 clients[obj.target].sock.send(JSON.stringify(
140 {code:"draw"}));
2cc10cdb 141 break;
5bd05dba
BA
142 }
143 });
144 socket.on("close", () => {
92a523d1 145 const page = clients[sid].page;
5bd05dba 146 delete clients[sid];
92a523d1 147 notifyRoom(page, "disconnect");
bcaa8c00
BA
148 if (page.indexOf("/game/") >= 0)
149 notifyRoom("/", "disconnect"); //notify main hall
5bd05dba
BA
150 });
151 });
1d184b4c 152}