With this, when a client joins they will get a snapshot of the current
game state as well as a history of all previous moves.
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.state.moves) {
- response.write(`event: move\ndata: ${move}\n\n`);
- }
+ /* When a new client joins, give them the current game state,
+ * (which includes the history of moves). */
+ const state_json = JSON.stringify(this.state);
+ response.write(`event: game-state\ndata: ${state_json}\n\n`);
}
}