c2fd552bc697c4fed96e982f375bb78463d44df9
[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) => {
25 Object.keys(clients).forEach(k => {
26 if (k != sid && clients[k].page == page)
27 {
28 clients[k].sock.send(JSON.stringify(Object.assign(
29 {code:code}, obj)));
30 }
31 });
32 };
33 notifyRoom(query["page"],"connect",{sid:sid});
34 socket.on("message", objtxt => {
35 let obj = JSON.parse(objtxt);
36 if (!!obj.target && !clients[obj.target])
37 return; //receiver not connected, nothing we can do
38 switch (obj.code)
39 {
40 case "pollclients":
41 const curPage = clients[sid].page;
42 socket.send(JSON.stringify({code:"pollclients",
43 sockIds: Object.keys(clients).filter(k =>
44 k != sid && clients[k].page == curPage)}));
45 break;
46 case "pagechange":
47 notifyRoom(clients[sid].page, "disconnect", {sid:sid});
48 clients[sid].page = obj.page;
49 notifyRoom(obj.page, "connect", {sid:sid});
50 break;
51 case "askidentity":
52 clients[obj.target].sock.send(JSON.stringify(
53 {code:"askidentity",from:sid}));
54 break;
55 case "askchallenge":
56 clients[obj.target].sock.send(JSON.stringify(
57 {code:"askchallenge",from:sid}));
58 break;
59 case "askgame":
60 clients[obj.target].sock.send(JSON.stringify(
61 {code:"askgame",from:sid}));
62 break;
63 case "identity":
64 clients[obj.target].sock.send(JSON.stringify(
65 {code:"identity",user:obj.user}));
66 break;
67 case "refusechallenge":
68 clients[obj.target].sock.send(JSON.stringify(
69 {code:"refusechallenge", cid:obj.cid, from:sid}));
70 break;
71 case "deletechallenge":
72 clients[obj.target].sock.send(JSON.stringify(
73 {code:"deletechallenge", cid:obj.cid, from:sid}));
74 break;
75 case "newgame":
76 clients[obj.target].sock.send(JSON.stringify(
77 {code:"newgame", gameInfo:obj.gameInfo, cid:obj.cid}));
78 break;
79 case "challenge":
80 clients[obj.target].sock.send(JSON.stringify(
81 {code:"challenge", chall:obj.chall, from:sid}));
82 break;
83 case "game":
84 clients[obj.target].sock.send(JSON.stringify(
85 {code:"game", game:obj.game, from:sid}));
86 break;
87 case "newchat":
88 notifyRoom(query["page"], "newchat",
89 {msg:obj.msg, name:obj.name, sid:sid})
90 break;
91 // TODO: WebRTC instead in this case (most demanding?)
92 case "newmove":
93 clients[obj.target].sock.send(JSON.stringify(
94 {code:"newmove",move:obj.move}));
95 break;
96 case "lastate":
97 clients[obj.target].sock.send(JSON.stringify(
98 {code:"lastate", state:obj.state}));
99 break;
100 case "resign":
101 clients[obj.target].sock.send(JSON.stringify(
102 {code:"resign"}));
103 break;
104 case "abort":
105 clients[obj.target].sock.send(JSON.stringify(
106 {code:"abort",msg:obj.msg}));
107 break;
108 case "drawoffer":
109 clients[obj.target].sock.send(JSON.stringify(
110 {code:"drawoffer"}));
111 break;
112 case "draw":
113 clients[obj.target].sock.send(JSON.stringify(
114 {code:"draw"}));
115 break;
116 }
117 });
118 socket.on("close", () => {
119 const page = clients[sid].page;
120 delete clients[sid];
121 notifyRoom(page, "disconnect");
122 });
123 });
124 }