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