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
] = {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
}));
30 notifyRoom(query
["page"],"connect");
31 socket
.on("message", objtxt
=> {
32 let obj
= JSON
.parse(objtxt
);
33 if (!!obj
.target
&& !clients
[obj
.target
])
34 return; //receiver not connected, nothing we can do
38 socket
.send(JSON
.stringify({code:"pollclients",
39 sockIds: Object
.keys(clients
).filter(k
=>
40 k
!= sid
&& clients
[k
].page
== obj
.page
)}));
43 notifyRoom(clients
[sid
].page
, "disconnect");
44 clients
[sid
].page
= obj
.page
;
45 notifyRoom(obj
.page
, "connect");
48 clients
[obj
.target
].sock
.send(
49 JSON
.stringify({code:"askidentity",from:sid
}));
52 clients
[obj
.target
].sock
.send(
53 JSON
.stringify({code:"askchallenge",from:sid
}));
56 clients
[obj
.target
].sock
.send(
57 JSON
.stringify({code:"askgame",from:sid
}));
60 clients
[obj
.target
].sock
.send(
61 JSON
.stringify({code:"identity",user:obj
.user
}));
63 case "refusechallenge":
64 clients
[obj
.target
].sock
.send(
65 JSON
.stringify({code:"refusechallenge", cid:obj
.cid
, from:sid
}));
67 case "deletechallenge":
68 clients
[obj
.target
].sock
.send(
69 JSON
.stringify({code:"deletechallenge", cid:obj
.cid
, from:sid
}));
72 clients
[obj
.target
].sock
.send(JSON
.stringify(
73 {code:"newgame", gameInfo:obj
.gameInfo
, cid:obj
.cid
}));
76 clients
[obj
.target
].sock
.send(JSON
.stringify(
77 {code:"challenge", chall:obj
.chall
, from:sid
}));
80 clients
[obj
.target
].sock
.send(JSON
.stringify(
81 {code:"game", game:obj
.game
, from:sid
}));
84 clients
[obj
.target
].sock
.send(JSON
.stringify({code:"newchat",msg:obj
.msg
}));
86 // TODO: WebRTC instead in this case (most demanding?)
88 clients
[obj
.target
].sock
.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
97 clients
[oppId
].sock
.send(JSON
.stringify(obj
));
100 clients
[obj
.target
].sock
.send(JSON
.stringify({code:"resign"}));
103 clients
[obj
.target
].sock
.send(JSON
.stringify({code:"abort",msg:obj
.msg
}));
106 clients
[obj
.target
].sock
.send(JSON
.stringify({code:"drawoffer"}));
109 clients
[obj
.target
].sock
.send(JSON
.stringify({code:"draw"}));
113 socket
.on("close", () => {
114 const page
= clients
[sid
].page
;
116 notifyRoom(page
, "disconnect");