- if (!!this.$route.params["id"])
- {
- this.gameRef.id = this.$route.params["id"];
- this.gameRef.rid = this.$route.query["rid"];
- this.loadGame();
- }
- // TODO: how to know who is observing ? Send message to everyone with game ID ?
- // and then just listen to (dis)connect events
- // server always send "connect on " + URL ; then add to observers if game...
- // detect multiple tabs connected (when connect ask server if my SID is already in use)
-// router when access a game page tell to server I joined + game ID (no need rid)
-// and ask server for current joined (= observers)
-// when send to chat (or a move), reach only this group (send gid along)
- // --> doivent être enregistrés comme observers au niveau du serveur...
- // non: poll users + events startObserving / stopObserving
- // (à faire au niveau du routeur ?)
-
- // TODO: also handle "draw accepted" (use opponents array?)
- // --> must give this info also when sending lastState...
- // and, if all players agree then OK draw (end game ...etc)
- const socketMessageListener = msg => {
+ // Always add myself to players' list
+ const my = this.st.user;
+ this.people.push({sid:my.sid, id:my.id, name:my.name});
+ this.gameRef.id = this.$route.params["id"];
+ this.gameRef.rid = this.$route.query["rid"]; //may be undefined
+ if (!this.gameRef.rid)
+ this.loadGame(); //local or corr: can load from now
+ // TODO: mode analyse (/analyze/Atomic/rn
+ // ... fen = query[], vname=params[] ...
+ // 0.1] Ask server for room composition:
+ const initialize = () => {
+ // Poll clients + load game if stored remotely
+ this.st.conn.send(JSON.stringify({code:"pollclients"}));
+ if (!!this.gameRef.rid)
+ this.loadGame();
+ };
+ if (!!this.st.conn && this.st.conn.readyState == 1) //1 == OPEN state
+ initialize();
+ else //socket not ready yet (initial loading)
+ this.st.conn.onopen = initialize;
+ this.st.conn.onmessage = this.socketMessageListener;
+ const socketCloseListener = () => {
+ store.socketCloseListener(); //reinitialize connexion (in store.js)
+ this.st.conn.addEventListener('message', this.socketMessageListener);
+ this.st.conn.addEventListener('close', socketCloseListener);
+ };
+ this.st.conn.onclose = socketCloseListener;
+ },
+ methods: {
+ getOppSid: function() {
+ if (!!this.game.oppsid)
+ return this.game.oppsid;
+ const opponent = this.people.find(p => p.id == this.game.oppid);
+ return (!!opponent ? opponent.sid : null);
+ },
+ socketMessageListener: function(msg) {