fcda981624f00650a8174dd5b8b79b5f0d15b090
1 const url
= require('url');
3 // Node version in Ubuntu 16.04 does not know about URL class
4 function getJsonFromUrl(url
)
6 const query
= url
.substr(2); //starts with "/?"
8 query
.split("&").forEach((part
) => {
9 const item
= part
.split("=");
10 result
[item
[0]] = decodeURIComponent(item
[1]);
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)
22 return socket
.send(JSON
.stringify({code:"duplicate"}));
23 clients
[sid
] = socket
;
25 Object
.keys(clients
).forEach(k
=> {
27 clients
[k
].send(JSON
.stringify({code:"connect",sid:sid
}));
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);
37 socket
.send(JSON
.stringify({code:"pollclients",
38 sockIds:Object
.keys(clients
).filter(k
=> k
!= sid
)}));
41 clients
[obj
.target
].send(
42 JSON
.stringify({code:"askidentity",from:sid
}));
45 clients
[obj
.target
].send(
46 JSON
.stringify({code:"askchallenge",from:sid
}));
49 clients
[obj
.target
].send(
50 JSON
.stringify({code:"askgame",from:sid
}));
53 clients
[obj
.target
].send(
54 JSON
.stringify({code:"identity",user:obj
.user
}));
57 clients
[obj
.target
].send(
58 JSON
.stringify({code:"challenge", chall:obj
.chall
, from:sid
}));
60 case "acceptchallenge":
61 clients
[obj
.target
].send(
62 JSON
.stringify({code:"acceptchallenge", cid:obj
.cid
, from:sid
}));
64 case "withdrawchallenge":
65 clients
[obj
.target
].send(
66 JSON
.stringify({code:"withdrawchallenge", cid:obj
.cid
, from:sid
}));
68 case "refusechallenge":
69 clients
[obj
.target
].send(
70 JSON
.stringify({code:"refusechallenge", cid:obj
.cid
, from:sid
}));
72 case "deletechallenge":
73 clients
[obj
.target
].send(
74 JSON
.stringify({code:"deletechallenge", cid:obj
.cid
, from:sid
}));
77 clients
[obj
.target
].send(JSON
.stringify(
78 {code:"newgame", gameInfo:obj
.gameInfo
, cid:obj
.cid
}));
81 // TODO: relay (live) game to other player
84 clients
[obj
.target
].send(JSON
.stringify({code:"newchat",msg:obj
.msg
}));
86 // TODO: WebRTC instead in this case (most demanding?)
88 clients
[obj
.target
].send(JSON
.stringify({code:"newmove",move:obj
.move}));
91 // If this code is reached, then obj.target is connected
92 socket
.send(JSON
.stringify({code:"pong"}));
95 const oppId
= obj
.target
;
96 obj
.oppid
= sid
; //I'm the opponent of my opponent(s)
97 clients
[oppId
].send(JSON
.stringify(obj
));
100 clients
[obj
.target
].send(JSON
.stringify({code:"resign"}));
103 clients
[obj
.target
].send(JSON
.stringify({code:"abort",msg:obj
.msg
}));
106 clients
[obj
.target
].send(JSON
.stringify({code:"drawoffer"}));
109 clients
[obj
.target
].send(JSON
.stringify({code:"draw"}));
113 socket
.on("close", () => {
115 // Notify every other connected client
116 Object
.keys(clients
).forEach( k
=> {
117 clients
[k
].send(JSON
.stringify({code:"disconnect",sid:sid
}));