From 212b5a93209741c091d2f504f3d2edac8ae98262 Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Fri, 29 May 2020 20:55:30 -0700 Subject: [PATCH] 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. --- tictactoe.js | 9 +++++++++ 1 file changed, 9 insertions(+) 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) => { -- 2.43.0