b7689d7af54b500d70993addff0977df8c268ba2
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 if (query
["page"].indexOf("/game/") >= 0)
37 notifyRoom("/", "connect"); //notify main hall
38 socket
.on("message", objtxt
=> {
39 let obj
= JSON
.parse(objtxt
);
40 if (!!obj
.target
&& !clients
[obj
.target
])
41 return; //receiver not connected, nothing we can do
43 console
.log(obj
.code
);
48 const curPage
= clients
[sid
].page
;
49 socket
.send(JSON
.stringify({code:"pollclients",
50 sockIds: Object
.keys(clients
).filter(k
=> k
!= sid
&&
51 (clients
[k
].page
== curPage
||
52 // Consider that people playing are in Hall too:
53 (curPage
== "/" && clients
[k
].page
.indexOf("/game/") >= 0))
57 notifyRoom(clients
[sid
].page
, "disconnect");
58 if (clients
[sid
].page
.indexOf("/game/") >= 0)
59 notifyRoom("/", "disconnect");
60 clients
[sid
].page
= obj
.page
;
61 notifyRoom(obj
.page
, "connect");
62 if (obj
.page
.indexOf("/game/") >= 0)
63 notifyRoom("/", "connect");
66 clients
[obj
.target
].sock
.send(JSON
.stringify(
67 {code:"askidentity",from:sid
}));
70 clients
[obj
.target
].sock
.send(JSON
.stringify(
71 {code:"askchallenge",from:sid
}));
74 // Check all clients playing, and send them a "askgame" message
75 Object
.keys(clients
).forEach(k
=> {
76 if (k
!= sid
&& clients
[k
].page
.indexOf("/game/") >= 0)
78 clients
[k
].sock
.send(JSON
.stringify(
79 {code:"askgame", from: sid
}));
82 clients
[obj
.target
].sock
.send(JSON
.stringify(
83 {code:"askgame",from:sid
}));
86 clients
[obj
.target
].sock
.send(JSON
.stringify(
87 {code:"identity",user:obj
.user
}));
89 case "refusechallenge":
90 clients
[obj
.target
].sock
.send(JSON
.stringify(
91 {code:"refusechallenge", cid:obj
.cid
, from:sid
}));
93 case "deletechallenge":
94 clients
[obj
.target
].sock
.send(JSON
.stringify(
95 {code:"deletechallenge", cid:obj
.cid
, from:sid
}));
98 clients
[obj
.target
].sock
.send(JSON
.stringify(
99 {code:"newgame", gameInfo:obj
.gameInfo
, cid:obj
.cid
}));
102 clients
[obj
.target
].sock
.send(JSON
.stringify(
103 {code:"challenge", chall:obj
.chall
, from:sid
}));
108 clients
[obj
.target
].sock
.send(JSON
.stringify(
109 {code:"game", game:obj
.game
, from:sid
}));
113 // Notify all room except opponent and me:
114 notifyRoom("/", "game", {game:obj
.game
}, [obj
.oppsid
]);
118 notifyRoom(query
["page"], "newchat", {msg:obj
.msg
, name:obj
.name
});
120 // TODO: WebRTC instead in this case (most demanding?)
122 clients
[obj
.target
].sock
.send(JSON
.stringify(
123 {code:"newmove", move:obj
.move}));
126 clients
[obj
.target
].sock
.send(JSON
.stringify(
127 {code:"lastate", state:obj
.state
}));
130 clients
[obj
.target
].sock
.send(JSON
.stringify(
134 clients
[obj
.target
].sock
.send(JSON
.stringify(
135 {code:"abort",msg:obj
.msg
}));
138 clients
[obj
.target
].sock
.send(JSON
.stringify(
139 {code:"drawoffer"}));
142 clients
[obj
.target
].sock
.send(JSON
.stringify(
147 socket
.on("close", () => {
148 const page
= clients
[sid
].page
;
150 notifyRoom(page
, "disconnect");
151 if (page
.indexOf("/game/") >= 0)
152 notifyRoom("/", "disconnect"); //notify main hall