+ // Associative array page --> sid --> tmpId --> socket
+ // "page" is either "/" for hall or "/game/some_gid" for Game,
+ // or "/mygames" for Mygames page (simpler: no 'people' array).
+ // tmpId is required if a same user (browser) has different tabs
+ let clients = {};
+ let sidToPages = {};
+ let idToSid = {};
+ wss.on("connection", (socket, req) => {
+ const query = getJsonFromUrl(req.url);
+ const sid = query["sid"];
+ const id = query["id"];
+ const tmpId = query["tmpId"];
+ const page = query["page"];
+ const notifyRoom = (page,code,obj={}) => {
+ if (!clients[page]) return;
+ Object.keys(clients[page]).forEach(k => {
+ Object.keys(clients[page][k]).forEach(x => {
+ if (k == sid && x == tmpId) return;
+ send(
+ clients[page][k][x].socket,
+ Object.assign({ code: code, from: sid }, obj)
+ );
+ });
+ });
+ };
+ // For focus events: no need to target self
+ const notifyAllBut = (page,code,obj={},except) => {
+ if (!clients[page]) return;
+ Object.keys(clients[page]).forEach(k => {
+ if (except.includes(k)) return;
+ Object.keys(clients[page][k]).forEach(x => {
+ send(
+ clients[page][k][x].socket,
+ Object.assign({ code: code, from: sid }, obj)
+ );
+ });
+ });
+ };
+ const deleteConnexion = () => {
+ if (!clients[page] || !clients[page][sid] || !clients[page][sid][tmpId])
+ return; //job already done
+ delete clients[page][sid][tmpId];
+ if (Object.keys(clients[page][sid]).length == 0) {
+ delete clients[page][sid];
+ const pgIndex = sidToPages[sid].findIndex(pg => pg == page);
+ sidToPages[sid].splice(pgIndex, 1);
+ if (Object.keys(clients[page]).length == 0)
+ delete clients[page];
+ // Am I totally offline?
+ if (sidToPages[sid].length == 0) {
+ delete sidToPages[sid];
+ delete idToSid[id];
+ }
+ }
+ };
+
+ const doDisconnect = () => {
+ deleteConnexion();
+ // Nothing to notify when disconnecting from MyGames page:
+ if (page != "/mygames" && (!clients[page] || !clients[page][sid])) {
+ // I effectively disconnected from this page:
+ notifyRoom(page, "disconnect");
+ if (page.indexOf("/game/") >= 0)
+ notifyRoom("/", "gdisconnect", { page:page });
+ }
+ };
+ const messageListener = (objtxt) => {
+ let obj = JSON.parse(objtxt);
+ switch (obj.code) {
+ // Wait for "connect" message to notify connection to the room,
+ // because if game loading is slow the message listener might
+ // not be ready too early.
+ case "connect": {
+ notifyRoom(page, "connect");
+ if (page.indexOf("/game/") >= 0)
+ notifyRoom("/", "gconnect", { page:page });
+ break;
+ }
+ case "disconnect":
+ // When page changes:
+ doDisconnect();
+ break;
+ 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 });
+ }
+ });
+ break;
+ }
+ case "pollclients": {
+ // From Hall or Game
+ let sockIds = [];
+ Object.keys(clients[page]).forEach(k => {
+ // Avoid polling myself: no new information to get
+ if (k != sid) sockIds.push(k);
+ });
+ send(socket, { code: "pollclients", sockIds: sockIds });
+ break;
+ }
+ case "pollclientsandgamers": {
+ // From Hall
+ let sockIds = [];
+ Object.keys(clients["/"]).forEach(k => {
+ // Avoid polling myself: no new information to get
+ if (k != sid) sockIds.push({sid:k});
+ });
+ // NOTE: a "gamer" could also just be an observer
+ Object.keys(clients).forEach(p => {
+ if (p.indexOf("/game/") >= 0) {
+ Object.keys(clients[p]).forEach(k => {
+ // 'page' indicator is needed for gamers
+ if (k != sid) sockIds.push({ sid:k, page:p });
+ });
+ }
+ });
+ send(socket, { code: "pollclientsandgamers", sockIds: sockIds });