From: Carl Worth Date: Sat, 30 May 2020 03:55:30 +0000 (-0700) Subject: tictactoe: Replay previous moves when a new client connects X-Git-Url: https://git.cworth.org/git?a=commitdiff_plain;ds=sidebyside;h=212b5a93209741c091d2f504f3d2edac8ae98262;hp=ea14cbbefb26afd558aff8329d92f1a7b9442bac;p=empires-server tictactoe: Replay previous moves when a new client connects This fixes the bug where a client would join a game in progress but would see only the blank board. --- diff --git a/tictactoe.js b/tictactoe.js index 79d7350..6d4b014 100644 --- a/tictactoe.js +++ b/tictactoe.js @@ -27,6 +27,15 @@ class TicTacToe extends Game { broadcast_move(square) { this.broadcast_event("move", square); } + + handle_events(request, response) { + super.handle_events(request, response); + + /* When a new client joins, replay all previous moves to it. */ + for (let move of this.moves) { + response.write(`event: move\ndata: ${move}\n\n`); + } + } } router.get('/', (request, response) => {