- case "askchallenge":
- clients[obj.target].sock.send(JSON.stringify(
- {code:"askchallenge",from:sid}));
- break;
- case "askgames":
- {
- // Check all clients playing, and send them a "askgame" message
- let gameSids = {}; //game ID --> [sid1, sid2]
- const regexpGid = /\/[a-zA-Z0-9]+$/;
- Object.keys(clients).forEach(k => {
- if (k != sid && clients[k].page.indexOf("/game/") >= 0)
- {
- const gid = clients[k].page.match(regexpGid)[0];
- if (!gameSids[gid])
- gameSids[gid] = [k];
- else
- gameSids[gid].push(k);
+ case "killme": {
+ // Self multi-connect: manual removal + disconnect
+ const doKill = (pg) => {
+ Object.keys(clients[pg][obj.sid]).forEach(x => {
+ send(clients[pg][obj.sid][x].socket, { code: "killed" });
+ });
+ delete clients[pg][obj.sid];
+ };
+ const disconnectFromOtherConnexion = (pg,code,o={}) => {
+ Object.keys(clients[pg]).forEach(k => {
+ if (k != obj.sid) {
+ Object.keys(clients[pg][k]).forEach(x => {
+ send(
+ clients[pg][k][x].socket,
+ Object.assign({ code: code, from: obj.sid }, o)
+ );
+ });
+ }
+ });
+ };
+ Object.keys(clients).forEach(pg => {
+ if (clients[pg][obj.sid]) {
+ doKill(pg);
+ disconnectFromOtherConnexion(pg, "disconnect");
+ if (pg.indexOf("/game/") >= 0 && clients["/"])
+ disconnectFromOtherConnexion("/", "gdisconnect", { page: pg });