b51a978aa2f30c2caa53ef14ad0e3d37a89fd93f
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
45 const curPage
= clients
[sid
].page
;
46 socket
.send(JSON
.stringify({code:"pollclients",
47 sockIds: Object
.keys(clients
).filter(k
=> k
!= sid
&&
48 (clients
[k
].page
== curPage
||
49 // Consider that people playing are in Hall too:
50 (curPage
== "/" && clients
[k
].page
.indexOf("/game/") >= 0))
54 notifyRoom(clients
[sid
].page
, "disconnect");
55 if (clients
[sid
].page
.indexOf("/game/") >= 0)
56 notifyRoom("/", "disconnect");
57 clients
[sid
].page
= obj
.page
;
58 notifyRoom(obj
.page
, "connect");
59 if (obj
.page
.indexOf("/game/") >= 0)
60 notifyRoom("/", "connect");
63 clients
[obj
.target
].sock
.send(JSON
.stringify(
64 {code:"askidentity",from:sid
}));
67 clients
[obj
.target
].sock
.send(JSON
.stringify(
68 {code:"askchallenge",from:sid
}));
72 // Check all clients playing, and send them a "askgame" message
73 let gameSids
= {}; //game ID --> [sid1, sid2]
74 const regexpGid
= /\/[a-zA-Z0-9]+$/;
75 Object
.keys(clients
).forEach(k
=> {
76 if (k
!= sid
&& clients
[k
].page
.indexOf("/game/") >= 0)
78 const gid
= clients
[k
].page
.match(regexpGid
)[0];
82 gameSids
[gid
].push(k
);
85 // Request only one client out of 2 (TODO: this is a bit heavy)
86 // Alt: ask game to all, and filter later?
87 Object
.keys(gameSids
).forEach(gid
=> {
88 const L
= gameSids
[gid
].length
;
90 ? Math
.floor(Math
.random() * Math
.floor(L
))
92 const rid
= gameSids
[gid
][idx
];
93 clients
[rid
].sock
.send(JSON
.stringify(
94 {code:"askgame", from: rid
}));
99 clients
[obj
.target
].sock
.send(JSON
.stringify(
100 {code:"askfullgame", from:sid
}));
103 clients
[obj
.target
].sock
.send(JSON
.stringify(
104 {code:"fullgame", game:obj
.game
}));
107 clients
[obj
.target
].sock
.send(JSON
.stringify(
108 {code:"identity",user:obj
.user
}));
110 case "refusechallenge":
111 clients
[obj
.target
].sock
.send(JSON
.stringify(
112 {code:"refusechallenge", cid:obj
.cid
, from:sid
}));
114 case "deletechallenge":
115 clients
[obj
.target
].sock
.send(JSON
.stringify(
116 {code:"deletechallenge", cid:obj
.cid
, from:sid
}));
119 clients
[obj
.target
].sock
.send(JSON
.stringify(
120 {code:"newgame", gameInfo:obj
.gameInfo
, cid:obj
.cid
}));
123 clients
[obj
.target
].sock
.send(JSON
.stringify(
124 {code:"challenge", chall:obj
.chall
, from:sid
}));
129 clients
[obj
.target
].sock
.send(JSON
.stringify(
130 {code:"game", game:obj
.game
, from:sid
}));
134 // Notify all room except opponent and me:
135 notifyRoom("/", "game", {game:obj
.game
}, [obj
.oppsid
]);
139 // WARNING: do not use query["page"], because the page may change
140 notifyRoom(clients
[sid
].page
, "newchat",
141 {msg: obj
.msg
, name: obj
.name
});
143 // TODO: WebRTC instead in this case (most demanding?)
145 clients
[obj
.target
].sock
.send(JSON
.stringify(
146 {code:"newmove", move:obj
.move}));
149 clients
[obj
.target
].sock
.send(JSON
.stringify(
150 {code:"lastate", state:obj
.state
}));
153 clients
[obj
.target
].sock
.send(JSON
.stringify(
157 clients
[obj
.target
].sock
.send(JSON
.stringify(
158 {code:"abort",msg:obj
.msg
}));
161 clients
[obj
.target
].sock
.send(JSON
.stringify(
162 {code:"drawoffer"}));
165 clients
[obj
.target
].sock
.send(JSON
.stringify(
170 socket
.on("close", () => {
171 const page
= clients
[sid
].page
;
173 notifyRoom(page
, "disconnect");
174 if (page
.indexOf("/game/") >= 0)
175 notifyRoom("/", "disconnect"); //notify main hall