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