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