-function handle_events(request, response) {
- const game = request.game;
- /* These headers will keep the connection open so we can stream events. */
- const headers = {
- "Content-type": "text/event-stream",
- "Connection": "keep-alive",
- "Cache-Control": "no-cache"
- };
- response.writeHead(200, headers);
-
- /* Now that a client has connected, first we need to stream all of
- * the existing spectators and players (if any). */
- if (game._spectators.length > 0) {
- const spectators_json = JSON.stringify(game.spectators);
- const spectators_data = `event: spectators\ndata: ${spectators_json}\n\n`;
- response.write(spectators_data);
- }