With this addition to the Game class, the TicTacToe class drops its
custom implementation of the handle_events method.
Similarly, future game implementations can simply have a "state"
property and not need to do anything further for new clients to be
provided with the current state when they join.
request.on('close', () => {
this.remove_client(id);
});
request.on('close', () => {
this.remove_client(id);
});
+
+ /* Finally, if this game class has a "state" property, stream that
+ * current state to the client. */
+ if (this.state) {
+ const state_json = JSON.stringify(this.state);
+ response.write(`event: game-state\ndata: ${state_json}\n\n`);
+ }
broadcast_move(square) {
this.broadcast_event("move", square);
}
broadcast_move(square) {
this.broadcast_event("move", square);
}
-
- handle_events(request, response) {
- super.handle_events(request, response);
-
- /* 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`);
- }
}
router.post('/move', (request, response) => {
}
router.post('/move', (request, response) => {