+ case "connect":
+ case "gconnect":
+ {
+ const page = data.page || "/";
+ // NOTE: player could have been polled earlier, but might have logged in then
+ // So it's a good idea to ask identity if he was anonymous.
+ // But only ask game / challenge if currently disconnected.
+ if (!this.people[data.from])
+ {
+ this.$set(this.people, data.from, {name:"", id:0, pages:[page]});
+ if (data.code == "connect")
+ this.send("askchallenge", {target:data.from});
+ else
+ this.send("askgame", {target:data.from, page:page});
+ }
+ else
+ {
+ // append page if not already in list
+ if (this.people[data.from].pages.indexOf(page) < 0)
+ this.people[data.from].pages.push(page);
+ }
+ if (this.people[data.from].id == 0)
+ {
+ this.newConnect[data.from] = true; //for self multi-connects tests
+ this.send("askidentity", {target:data.from, page:page});
+ }
+ break;
+ }
+ case "disconnect":
+ case "gdisconnect":
+ // If the user reloads the page twice very quickly (experienced with Firefox),
+ // the first reload won't have time to connect but will trigger a "close" event anyway.
+ // ==> Next check is required.
+ if (!this.people[data.from])
+ return;
+ // Disconnect means no more tmpIds:
+ if (data.code == "disconnect")
+ {
+ // Remove the live challenge sent by this player:
+ ArrayFun.remove(this.challenges, c => c.from.sid == data.from);
+ }
+ else
+ {
+ // Remove the matching live game if now unreachable
+ const gid = data.page.match(/[a-zA-Z0-9]+$/)[0];
+ const gidx = this.games.findIndex(g => g.id == gid);
+ if (gidx >= 0)
+ {
+ const game = this.games[gidx];
+ if (game.type == "live" &&
+ game.rids.length == 1 && game.rids[0] == data.from)
+ {
+ this.games.splice(gidx, 1);
+ }
+ }
+ }
+ const page = data.page || "/";
+ ArrayFun.remove(this.people[data.from].pages, p => p == page);
+ if (this.people[data.from].pages.length == 0)
+ this.$delete(this.people, data.from);
+ break;
+ case "killed":
+ // I logged in elsewhere:
+ alert(this.st.tr["New connexion detected: tab now offline"]);
+ // TODO: this fails. See https://github.com/websockets/ws/issues/489
+ //this.conn.removeEventListener("message", this.socketMessageListener);
+ //this.conn.removeEventListener("close", this.socketCloseListener);
+ //this.conn.close();
+ this.conn = null;
+ break;