Fix chat issues when launching a game
[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 // TODO: later, allow duplicate connections (shouldn't be much more complicated)
21 if (!!clients[sid])
22 return socket.send(JSON.stringify({code:"duplicate"}));
23 clients[sid] = {sock: socket, page: query["page"]};
24 const notifyRoom = (page,code,obj={},excluded=[]) => {
25 Object.keys(clients).forEach(k => {
26 if (k in excluded)
27 return;
28 if (k != sid && clients[k].page == page)
29 {
30 clients[k].sock.send(JSON.stringify(Object.assign(
31 {code:code, from:sid}, obj)));
32 }
33 });
34 };
35 notifyRoom(query["page"], "connect"); //Hall or Game
36 if (query["page"].indexOf("/game/") >= 0)
37 notifyRoom("/", "connect"); //notify main hall
38 socket.on("message", objtxt => {
39 let obj = JSON.parse(objtxt);
40 if (!!obj.target && !clients[obj.target])
41 return; //receiver not connected, nothing we can do
42 switch (obj.code)
43 {
44 case "pollclients":
45 const curPage = clients[sid].page;
46 socket.send(JSON.stringify({code:"pollclients",
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 )}));
52 break;
53 case "pagechange":
54 notifyRoom(clients[sid].page, "disconnect");
55 if (clients[sid].page.indexOf("/game/") >= 0)
56 notifyRoom("/", "disconnect");
57 clients[sid].page = obj.page;
58 notifyRoom(obj.page, "connect");
59 if (obj.page.indexOf("/game/") >= 0)
60 notifyRoom("/", "connect");
61 break;
62 case "askidentity":
63 clients[obj.target].sock.send(JSON.stringify(
64 {code:"askidentity",from:sid}));
65 break;
66 case "askchallenge":
67 clients[obj.target].sock.send(JSON.stringify(
68 {code:"askchallenge",from:sid}));
69 break;
70 case "askgames":
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 });
79 break;
80 case "identity":
81 clients[obj.target].sock.send(JSON.stringify(
82 {code:"identity",user:obj.user}));
83 break;
84 case "refusechallenge":
85 clients[obj.target].sock.send(JSON.stringify(
86 {code:"refusechallenge", cid:obj.cid, from:sid}));
87 break;
88 case "deletechallenge":
89 clients[obj.target].sock.send(JSON.stringify(
90 {code:"deletechallenge", cid:obj.cid, from:sid}));
91 break;
92 case "newgame":
93 clients[obj.target].sock.send(JSON.stringify(
94 {code:"newgame", gameInfo:obj.gameInfo, cid:obj.cid}));
95 break;
96 case "challenge":
97 clients[obj.target].sock.send(JSON.stringify(
98 {code:"challenge", chall:obj.chall, from:sid}));
99 break;
100 case "game":
101 if (!!obj.target)
102 {
103 clients[obj.target].sock.send(JSON.stringify(
104 {code:"game", game:obj.game, from:sid}));
105 }
106 else
107 {
108 // Notify all room except opponent and me:
109 notifyRoom("/", "game", {game:obj.game}, [obj.oppsid]);
110 }
111 break;
112 case "newchat":
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});
116 break;
117 // TODO: WebRTC instead in this case (most demanding?)
118 case "newmove":
119 clients[obj.target].sock.send(JSON.stringify(
120 {code:"newmove", move:obj.move}));
121 break;
122 case "lastate":
123 clients[obj.target].sock.send(JSON.stringify(
124 {code:"lastate", state:obj.state}));
125 break;
126 case "resign":
127 clients[obj.target].sock.send(JSON.stringify(
128 {code:"resign"}));
129 break;
130 case "abort":
131 clients[obj.target].sock.send(JSON.stringify(
132 {code:"abort",msg:obj.msg}));
133 break;
134 case "drawoffer":
135 clients[obj.target].sock.send(JSON.stringify(
136 {code:"drawoffer"}));
137 break;
138 case "draw":
139 clients[obj.target].sock.send(JSON.stringify(
140 {code:"draw"}));
141 break;
142 }
143 });
144 socket.on("close", () => {
145 const page = clients[sid].page;
146 delete clients[sid];
147 notifyRoom(page, "disconnect");
148 if (page.indexOf("/game/") >= 0)
149 notifyRoom("/", "disconnect"); //notify main hall
150 });
151 });
152 }