c2fd552bc697c4fed96e982f375bb78463d44df9
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
,obj
) => {
25 Object
.keys(clients
).forEach(k
=> {
26 if (k
!= sid
&& clients
[k
].page
== page
)
28 clients
[k
].sock
.send(JSON
.stringify(Object
.assign(
33 notifyRoom(query
["page"],"connect",{sid:sid
});
34 socket
.on("message", objtxt
=> {
35 let obj
= JSON
.parse(objtxt
);
36 if (!!obj
.target
&& !clients
[obj
.target
])
37 return; //receiver not connected, nothing we can do
41 const curPage
= clients
[sid
].page
;
42 socket
.send(JSON
.stringify({code:"pollclients",
43 sockIds: Object
.keys(clients
).filter(k
=>
44 k
!= sid
&& clients
[k
].page
== curPage
)}));
47 notifyRoom(clients
[sid
].page
, "disconnect", {sid:sid
});
48 clients
[sid
].page
= obj
.page
;
49 notifyRoom(obj
.page
, "connect", {sid:sid
});
52 clients
[obj
.target
].sock
.send(JSON
.stringify(
53 {code:"askidentity",from:sid
}));
56 clients
[obj
.target
].sock
.send(JSON
.stringify(
57 {code:"askchallenge",from:sid
}));
60 clients
[obj
.target
].sock
.send(JSON
.stringify(
61 {code:"askgame",from:sid
}));
64 clients
[obj
.target
].sock
.send(JSON
.stringify(
65 {code:"identity",user:obj
.user
}));
67 case "refusechallenge":
68 clients
[obj
.target
].sock
.send(JSON
.stringify(
69 {code:"refusechallenge", cid:obj
.cid
, from:sid
}));
71 case "deletechallenge":
72 clients
[obj
.target
].sock
.send(JSON
.stringify(
73 {code:"deletechallenge", cid:obj
.cid
, from:sid
}));
76 clients
[obj
.target
].sock
.send(JSON
.stringify(
77 {code:"newgame", gameInfo:obj
.gameInfo
, cid:obj
.cid
}));
80 clients
[obj
.target
].sock
.send(JSON
.stringify(
81 {code:"challenge", chall:obj
.chall
, from:sid
}));
84 clients
[obj
.target
].sock
.send(JSON
.stringify(
85 {code:"game", game:obj
.game
, from:sid
}));
88 notifyRoom(query
["page"], "newchat",
89 {msg:obj
.msg
, name:obj
.name
, sid:sid
})
91 // TODO: WebRTC instead in this case (most demanding?)
93 clients
[obj
.target
].sock
.send(JSON
.stringify(
94 {code:"newmove",move:obj
.move}));
97 clients
[obj
.target
].sock
.send(JSON
.stringify(
98 {code:"lastate", state:obj
.state
}));
101 clients
[obj
.target
].sock
.send(JSON
.stringify(
105 clients
[obj
.target
].sock
.send(JSON
.stringify(
106 {code:"abort",msg:obj
.msg
}));
109 clients
[obj
.target
].sock
.send(JSON
.stringify(
110 {code:"drawoffer"}));
113 clients
[obj
.target
].sock
.send(JSON
.stringify(
118 socket
.on("close", () => {
119 const page
= clients
[sid
].page
;
121 notifyRoom(page
, "disconnect");