+ atCreation: function() {
+ // 0] (Re)Set variables
+ this.gameRef.id = this.$route.params["id"];
+ // rid = remote ID to find an observed live game,
+ // next = next corr games IDs to navigate faster
+ // (Both might be undefined)
+ this.gameRef.rid = this.$route.query["rid"];
+ this.nextIds = JSON.parse(this.$route.query["next"] || "[]");
+ // Always add myself to players' list
+ const my = this.st.user;
+ this.$set(this.people, my.sid, { id: my.id, name: my.name });
+ this.game = {
+ players: [{ name: "" }, { name: "" }],
+ chats: [],
+ rendered: false
+ };
+ let chatComp = this.$refs["chatcomp"];
+ if (!!chatComp) chatComp.chats = [];
+ this.virtualClocks = [[0,0], [0,0]];
+ this.vr = null;
+ this.drawOffer = "";
+ this.onMygames = [];
+ this.lastate = undefined;
+ this.newChat = "";
+ this.roomInitialized = false;
+ this.askGameTime = 0;
+ this.gameIsLoading = false;
+ this.gotLastate = false;
+ this.gotMoveIdx = -1;
+ this.opponentGotMove = false;
+ this.askIfPeerConnected = null;
+ this.askLastate = null;
+ this.retrySendmove = null;
+ this.clockUpdate = null;
+ this.newConnect = {};
+ this.killed = {};
+ // 1] Initialize connection
+ this.connexionString =
+ params.socketUrl +
+ "/?sid=" +
+ this.st.user.sid +
+ "&tmpId=" +
+ getRandString() +
+ "&page=" +
+ // Discard potential "/?next=[...]" for page indication:
+ encodeURIComponent(this.$route.path.match(/\/game\/[a-zA-Z0-9]+/)[0]);
+ this.conn = new WebSocket(this.connexionString);
+ this.conn.onmessage = this.socketMessageListener;
+ this.conn.onclose = this.socketCloseListener;
+ // Socket init required before loading remote game:
+ const socketInit = callback => {
+ if (!!this.conn && this.conn.readyState == 1)
+ // 1 == OPEN state
+ callback();
+ else
+ // Socket not ready yet (initial loading)
+ // NOTE: it's important to call callback without arguments,
+ // otherwise first arg is Websocket object and loadGame fails.
+ this.conn.onopen = () => callback();
+ };
+ if (!this.gameRef.rid)
+ // Game stored locally or on server
+ this.loadGame(null, () => socketInit(this.roomInit));
+ else
+ // Game stored remotely: need socket to retrieve it
+ // NOTE: the callback "roomInit" will be lost, so we don't provide it.
+ // --> It will be given when receiving "fullgame" socket event.
+ socketInit(this.loadGame);
+ },
+ cleanBeforeDestroy: function() {
+ if (!!this.askIfPeerConnected)
+ clearInterval(this.askIfPeerConnected);
+ if (!!this.askLastate)
+ clearInterval(this.askLastate);
+ if (!!this.retrySendmove)
+ clearInterval(this.retrySendmove);
+ if (!!this.clockUpdate)
+ clearInterval(this.clockUpdate);
+ this.send("disconnect");
+ },