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
={},excluded
=[]) => {
25 Object
.keys(clients
).forEach(k
=> {
28 if (k
!= sid
&& clients
[k
].page
== page
)
30 clients
[k
].sock
.send(JSON
.stringify(Object
.assign(
31 {code:code
, from:sid
}, obj
)));
35 notifyRoom(query
["page"], "connect"); //Hall or Game
36 socket
.on("message", objtxt
=> {
37 let obj
= JSON
.parse(objtxt
);
38 if (!!obj
.target
&& !clients
[obj
.target
])
39 return; //receiver not connected, nothing we can do
41 console
.log(obj
.code
);
47 const curPage
= clients
[sid
].page
;
48 socket
.send(JSON
.stringify({code:"pollclients",
49 sockIds: Object
.keys(clients
).filter(k
=> k
!= sid
&&
50 (clients
[k
].page
== curPage
||
51 // Consider that people playing are in Hall too:
52 (curPage
== "/" && clients
[k
].page
.indexOf("/game/") >= 0))
56 notifyRoom(clients
[sid
].page
, "disconnect");
57 clients
[sid
].page
= obj
.page
;
58 notifyRoom(obj
.page
, "connect");
61 clients
[obj
.target
].sock
.send(JSON
.stringify(
62 {code:"askidentity",from:sid
}));
65 clients
[obj
.target
].sock
.send(JSON
.stringify(
66 {code:"askchallenge",from:sid
}));
69 // Check all clients playing, and send them a "askgame" message
70 Object
.keys(clients
).forEach(k
=> {
71 if (k
!= sid
&& clients
[k
].page
.indexOf("/game/") >= 0)
73 clients
[k
].sock
.send(JSON
.stringify(
74 {code:"askgame", from: sid
}));
77 clients
[obj
.target
].sock
.send(JSON
.stringify(
78 {code:"askgame",from:sid
}));
81 clients
[obj
.target
].sock
.send(JSON
.stringify(
82 {code:"identity",user:obj
.user
}));
84 case "refusechallenge":
85 clients
[obj
.target
].sock
.send(JSON
.stringify(
86 {code:"refusechallenge", cid:obj
.cid
, from:sid
}));
88 case "deletechallenge":
89 clients
[obj
.target
].sock
.send(JSON
.stringify(
90 {code:"deletechallenge", cid:obj
.cid
, from:sid
}));
93 clients
[obj
.target
].sock
.send(JSON
.stringify(
94 {code:"newgame", gameInfo:obj
.gameInfo
, cid:obj
.cid
}));
97 clients
[obj
.target
].sock
.send(JSON
.stringify(
98 {code:"challenge", chall:obj
.chall
, from:sid
}));
103 clients
[obj
.target
].sock
.send(JSON
.stringify(
104 {code:"game", game:obj
.game
, from:sid
}));
108 // Notify all room except opponent and me:
109 notifyRoom("/", "game", {game:obj
.game
}, [obj
.oppsid
]);
113 notifyRoom(query
["page"], "newchat", {msg:obj
.msg
, name:obj
.name
});
115 // TODO: WebRTC instead in this case (most demanding?)
117 clients
[obj
.target
].sock
.send(JSON
.stringify(
118 {code:"newmove", move:obj
.move}));
121 clients
[obj
.target
].sock
.send(JSON
.stringify(
122 {code:"lastate", state:obj
.state
}));
125 clients
[obj
.target
].sock
.send(JSON
.stringify(
129 clients
[obj
.target
].sock
.send(JSON
.stringify(
130 {code:"abort",msg:obj
.msg
}));
133 clients
[obj
.target
].sock
.send(JSON
.stringify(
134 {code:"drawoffer"}));
137 clients
[obj
.target
].sock
.send(JSON
.stringify(
142 socket
.on("close", () => {
143 const page
= clients
[sid
].page
;
145 notifyRoom(page
, "disconnect");