+ add_spectator(name, session_id) {
+ /* Don't add another spectator that matches an existing session. */
+ const existing = this._spectators.findIndex(
+ spectator => spectator.session_id === session_id);
+ if (existing >= 0)
+ return existing.id;
+
+ const new_spectator = {id: this.next_spectator_id,
+ name: name,
+ session_id: session_id
+ };
+ this._spectators.push(new_spectator);
+ this.next_spectator_id++;
+ this.broadcast_event("spectator-join", JSON.stringify(new_spectator));
+
+ return new_spectator.id;
+ }
+
+ remove_spectator(id) {
+ const index = this._spectators.findIndex(spectator => spectator.id === id);
+ this._spectators.splice(index, 1);
+
+ this.broadcast_event("spectator-leave", `{"id": ${id}}`);
+ }
+