- case "pollclients":
- const curPage = clients[sid].page;
- socket.send(JSON.stringify({code:"pollclients",
- sockIds: Object.keys(clients).filter(k =>
- k != sid && clients[k].page == curPage)}));
- break;
- case "pagechange":
- notifyRoom(clients[sid].page, "disconnect");
- clients[sid].page = obj.page;
- notifyRoom(obj.page, "connect");
- break;
+ // 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], {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], 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 != "/")
+ {
+ Object.keys(clients[p]).forEach(k => {
+ if (k != sid)
+ sockIds.push({sid:k, page:p}); //page needed for gamers
+ });
+ }
+ });
+ send(socket, {code:"pollclientsandgamers", sockIds:sockIds});
+ break;
+ }
+
+ // Asking something: from is fully identified,
+ // but the requested resource can be from any tmpId (except current!)