X-Git-Url: https://git.auder.net/?p=vchess.git;a=blobdiff_plain;f=client%2Fsrc%2Fviews%2FMyGames.vue;h=e0914f67c1ed63f07e9afdcca5bf295baaa34606;hp=41cced56f55d8ba78c1d85fe1f6fb8b0ef656483;hb=db1f1f9adb920605c7a16b060a7737e54636ee08;hpb=e891730f77312340d68bb7ef72a4207541fa3914 diff --git a/client/src/views/MyGames.vue b/client/src/views/MyGames.vue index 41cced56..e0914f67 100644 --- a/client/src/views/MyGames.vue +++ b/client/src/views/MyGames.vue @@ -34,11 +34,13 @@ export default { st: store.state, display: "live", liveGames: [], - corrGames: [] + corrGames: [], + conn: null, + connexionString: "" }; }, created: function() { - GameStorage.getAll(localGames => { + GameStorage.getAll(true, localGames => { localGames.forEach(g => (g.type = this.classifyObject(g))); this.liveGames = localGames; }); @@ -48,6 +50,18 @@ export default { this.corrGames = res.games; }); } + // Initialize connection + this.connexionString = + params.socketUrl + + "/?sid=" + + this.st.user.sid + + "&tmpId=" + + getRandString() + + "&page=" + + encodeURIComponent(this.$route.path); + this.conn = new WebSocket(this.connexionString); + this.conn.onmessage = this.socketMessageListener; + this.conn.onclose = this.socketCloseListener; }, mounted: function() { const showType = localStorage.getItem("type-myGames") || "live"; @@ -69,6 +83,23 @@ export default { }, showGame: function(g) { this.$router.push("/game/" + g.id); + }, + socketMessageListener: function(msg) { + const data = JSON.parse(msg.data); + // Only event is newmove, and received only: + if (data.code == "newmove") { + let games = !!parseInt(data.gid) + ? this.corrGames + : this.liveGames; + // NOTE: new move itself is not received, because it wouldn't be used. + let g = games.find(g => g.id == data.gid); + this.$set(g, "movesCount", g.movesCount + 1); + } + }, + socketCloseListener: function() { + this.conn = new WebSocket(this.connexionString); + this.conn.addEventListener("message", this.socketMessageListener); + this.conn.addEventListener("close", this.socketCloseListener); } } };