b7689d7af54b500d70993addff0977df8c268ba2
[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
43 console.log(obj.code);
44
45 switch (obj.code)
46 {
47 case "pollclients":
48 const curPage = clients[sid].page;
49 socket.send(JSON.stringify({code:"pollclients",
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 )}));
55 break;
56 case "pagechange":
57 notifyRoom(clients[sid].page, "disconnect");
58 if (clients[sid].page.indexOf("/game/") >= 0)
59 notifyRoom("/", "disconnect");
60 clients[sid].page = obj.page;
61 notifyRoom(obj.page, "connect");
62 if (obj.page.indexOf("/game/") >= 0)
63 notifyRoom("/", "connect");
64 break;
65 case "askidentity":
66 clients[obj.target].sock.send(JSON.stringify(
67 {code:"askidentity",from:sid}));
68 break;
69 case "askchallenge":
70 clients[obj.target].sock.send(JSON.stringify(
71 {code:"askchallenge",from:sid}));
72 break;
73 case "askgame":
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 });
82 clients[obj.target].sock.send(JSON.stringify(
83 {code:"askgame",from:sid}));
84 break;
85 case "identity":
86 clients[obj.target].sock.send(JSON.stringify(
87 {code:"identity",user:obj.user}));
88 break;
89 case "refusechallenge":
90 clients[obj.target].sock.send(JSON.stringify(
91 {code:"refusechallenge", cid:obj.cid, from:sid}));
92 break;
93 case "deletechallenge":
94 clients[obj.target].sock.send(JSON.stringify(
95 {code:"deletechallenge", cid:obj.cid, from:sid}));
96 break;
97 case "newgame":
98 clients[obj.target].sock.send(JSON.stringify(
99 {code:"newgame", gameInfo:obj.gameInfo, cid:obj.cid}));
100 break;
101 case "challenge":
102 clients[obj.target].sock.send(JSON.stringify(
103 {code:"challenge", chall:obj.chall, from:sid}));
104 break;
105 case "game":
106 if (!!obj.target)
107 {
108 clients[obj.target].sock.send(JSON.stringify(
109 {code:"game", game:obj.game, from:sid}));
110 }
111 else
112 {
113 // Notify all room except opponent and me:
114 notifyRoom("/", "game", {game:obj.game}, [obj.oppsid]);
115 }
116 break;
117 case "newchat":
118 notifyRoom(query["page"], "newchat", {msg:obj.msg, name:obj.name});
119 break;
120 // TODO: WebRTC instead in this case (most demanding?)
121 case "newmove":
122 clients[obj.target].sock.send(JSON.stringify(
123 {code:"newmove", move:obj.move}));
124 break;
125 case "lastate":
126 clients[obj.target].sock.send(JSON.stringify(
127 {code:"lastate", state:obj.state}));
128 break;
129 case "resign":
130 clients[obj.target].sock.send(JSON.stringify(
131 {code:"resign"}));
132 break;
133 case "abort":
134 clients[obj.target].sock.send(JSON.stringify(
135 {code:"abort",msg:obj.msg}));
136 break;
137 case "drawoffer":
138 clients[obj.target].sock.send(JSON.stringify(
139 {code:"drawoffer"}));
140 break;
141 case "draw":
142 clients[obj.target].sock.send(JSON.stringify(
143 {code:"draw"}));
144 break;
145 }
146 });
147 socket.on("close", () => {
148 const page = clients[sid].page;
149 delete clients[sid];
150 notifyRoom(page, "disconnect");
151 if (page.indexOf("/game/") >= 0)
152 notifyRoom("/", "disconnect"); //notify main hall
153 });
154 });
155 }