09434a02b75ea53228d61f0a91ca606459e8a577
[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 //console.log(obj.code);
34 switch (obj.code)
35 {
36 case "pollclients":
37 socket.send(JSON.stringify({code:"pollclients",
38 sockIds:Object.keys(clients).filter(k => k != sid)}));
39 break;
40 case "askidentity":
41 clients[obj.target].send(
42 JSON.stringify({code:"askidentity",from:sid}));
43 break;
44 case "askchallenge":
45 clients[obj.target].send(
46 JSON.stringify({code:"askchallenge",from:sid}));
47 break;
48 case "askgame":
49 clients[obj.target].send(
50 JSON.stringify({code:"askgame",from:sid}));
51 break;
52 case "identity":
53 clients[obj.target].send(
54 JSON.stringify({code:"identity",user:obj.user}));
55 break;
56 case "challenge":
57 clients[obj.target].send(
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}));
63 break;
64 case "withdrawchallenge":
65 clients[obj.target].send(
66 JSON.stringify({code:"withdrawchallenge", cid:obj.cid, from:sid}));
67 break;
68 case "refusechallenge":
69 clients[obj.target].send(
70 JSON.stringify({code:"refusechallenge", cid:obj.cid, from:sid}));
71 break;
72 case "deletechallenge":
73 clients[obj.target].send(
74 JSON.stringify({code:"deletechallenge", cid:obj.cid, from:sid}));
75 break;
76 case "newgame":
77 clients[obj.target].send(JSON.stringify(
78 {code:"newgame", gameInfo:obj.gameInfo, cid:obj.cid}));
79 break;
80 case "game":
81 // TODO: relay (live) game to other player
82 break;
83 case "newchat":
84 clients[obj.target].send(JSON.stringify({code:"newchat",msg:obj.msg}));
85 break;
86 // Transmit chats and moves to current room
87 // TODO: WebRTC instead in this case (most demanding?)
88 case "newmove":
89 clients[obj.target].send(JSON.stringify({code:"newmove",move:obj.move}));
90 break;
91 // TODO: generalize that for several opponents
92 case "ping":
93 socket.send(JSON.stringify({code:"pong",gameId:obj.gameId}));
94 break;
95 case "lastate":
96 const oppId = obj.target;
97 obj.oppid = sid; //I'm the opponent of my opponent(s)
98 clients[oppId].send(JSON.stringify(obj));
99 break;
100 case "resign":
101 clients[obj.target].send(JSON.stringify({code:"resign"}));
102 break;
103 case "abort":
104 clients[obj.target].send(JSON.stringify({code:"abort",msg:obj.msg}));
105 break;
106 }
107 });
108 socket.on("close", () => {
109 delete clients[sid];
110 // Notify every other connected client
111 Object.keys(clients).forEach( k => {
112 clients[k].send(JSON.stringify({code:"disconnect",sid:sid}));
113 });
114 });
115 });
116 }