Cosmetics
[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"}));
23 clients[sid] = socket;
24 // Notify room:
5a3da968
BA
25 Object.keys(clients).forEach(k => {
26 if (k != sid)
27 clients[k].send(JSON.stringify({code:"connect",sid:sid}));
28 });
29 socket.on("message", objtxt => {
5bd05dba 30 let obj = JSON.parse(objtxt);
5a3da968 31 if (!!obj.target && !clients[obj.target])
b4d619d1 32 return; //receiver not connected, nothing we can do
5a3da968 33 //console.log(obj.code);
5bd05dba
BA
34 switch (obj.code)
35 {
81d9ce72
BA
36 case "pollclients":
37 socket.send(JSON.stringify({code:"pollclients",
38 sockIds:Object.keys(clients).filter(k => k != sid)}));
5a3da968
BA
39 break;
40 case "askidentity":
81d9ce72
BA
41 clients[obj.target].send(
42 JSON.stringify({code:"askidentity",from:sid}));
43 break;
dd75774d 44 case "askchallenge":
81d9ce72 45 clients[obj.target].send(
dd75774d 46 JSON.stringify({code:"askchallenge",from:sid}));
81d9ce72
BA
47 break;
48 case "askgame":
49 clients[obj.target].send(
50 JSON.stringify({code:"askgame",from:sid}));
5a3da968
BA
51 break;
52 case "identity":
81d9ce72
BA
53 clients[obj.target].send(
54 JSON.stringify({code:"identity",user:obj.user}));
4d64881e 55 break;
dd75774d 56 case "challenge":
6855163c 57 clients[obj.target].send(
bb7dd7db
BA
58 JSON.stringify({code:"challenge", chall:obj.chall, from:sid}));
59 break;
60 case "acceptchallenge":
61 clients[obj.target].send(
62 JSON.stringify({code:"acceptchallenge", cid:obj.cid, from:sid}));
dd75774d 63 break;
a6bddfc6
BA
64 case "withdrawchallenge":
65 clients[obj.target].send(
66 JSON.stringify({code:"withdrawchallenge", cid:obj.cid, from:sid}));
67 break;
5bd05dba
BA
68 case "refusechallenge":
69 clients[obj.target].send(
70 JSON.stringify({code:"refusechallenge", cid:obj.cid, from:sid}));
71 break;
98f48579
BA
72 case "deletechallenge":
73 clients[obj.target].send(
74 JSON.stringify({code:"deletechallenge", cid:obj.cid, from:sid}));
75 break;
a6bddfc6
BA
76 case "newgame":
77 clients[obj.target].send(JSON.stringify(
5bd05dba 78 {code:"newgame", gameInfo:obj.gameInfo, cid:obj.cid}));
a6bddfc6 79 break;
dd75774d 80 case "game":
bb7dd7db 81 // TODO: relay (live) game to other player
4d64881e 82 break;
5bd05dba 83 case "newchat":
5a3da968 84 clients[obj.target].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":
5a3da968 88 clients[obj.target].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
BA
95 const oppId = obj.target;
96 obj.oppid = sid; //I'm the opponent of my opponent(s)
4d64881e 97 clients[oppId].send(JSON.stringify(obj));
5bd05dba 98 break;
5bd05dba 99 case "resign":
5a3da968 100 clients[obj.target].send(JSON.stringify({code:"resign"}));
5bd05dba 101 break;
b988c726
BA
102 case "abort":
103 clients[obj.target].send(JSON.stringify({code:"abort",msg:obj.msg}));
104 break;
5bd05dba
BA
105 }
106 });
107 socket.on("close", () => {
108 delete clients[sid];
b4d619d1
BA
109 // Notify every other connected client
110 Object.keys(clients).forEach( k => {
4d64881e 111 clients[k].send(JSON.stringify({code:"disconnect",sid:sid}));
b4d619d1 112 });
5bd05dba
BA
113 });
114 });
1d184b4c 115}