object from new_player with just the subset of fields that we
want. */
const player_string = JSON.stringify((({id, name}) => ({id, name}))(new_player));
- this.broadcast("player-register", player_string);
+ this.broadcast_event("player-register", player_string);
}
remove_player(id) {
const index = this._players.findIndex(player => player.id === id);
this._players.splice(index, 1);
- this.broadcast("player-deregister", `{"id": ${id}}`);
+ this.broadcast_event("player-deregister", `{"id": ${id}}`);
}
remove_all_players() {
let captor = this._players.find(player => player.id === captor_id);
captor.captures.push(captee_id);
- this.broadcast("capture", `{"captor": ${captor_id}, "captee": ${captee_id}}`);
+ this.broadcast_event("capture", `{"captor": ${captor_id}, "captee": ${captee_id}}`);
}
liberate(captee_id) {
this.clients = this.clients.filter(client => client.id !== id);
}
- /* Send an event to all clients */
- broadcast(type, data) {
- this.clients.forEach(client => client.response.write(`event: ${type}\ndata: ${data}\n\n`));
+ /* Send a string to all clients */
+ broadcast_string(str) {
+ this.clients.forEach(client => client.response.write(str + '\n'));
+ }
+
+ /* Send an event to all clients.
+ *
+ * An event has both a declared type and a separate data block.
+ * It also ends with two newlines (to mark the end of the event).
+ */
+ broadcast_event(type, data) {
+ this.broadcast_string(`event: ${type}\ndata: ${data}\n`);
}
}