1 const fs = require('fs');
3 const express = require('express');
5 const http = require('http');
6 const server = http.createServer(app);
7 const { Server } = require("socket.io");
8 const io = new Server(server);
13 // Save comments when server is shutting down
15 fs.writeFileSync('zombocom-state.json', JSON.stringify(comments), (error) => {
21 // And connect to that on either clean exit...
22 process.on('exit', cleanup);
24 // ... or on a SIGINT (control-C)
25 process.on('SIGINT', () => {
30 app.get('/index.html', (req, res) => {
31 res.sendFile(__dirname + '/index.html');
34 io.on('connection', (socket) => {
35 // Replay old comments to a newly-joining client
36 comments.forEach((comment) => {
37 socket.emit('comment', comment)
39 // When any client comments, send that to all clients (including sender)
40 socket.on('comment', (comment) => {
41 io.emit('comment', comment);
42 comments.push(comment);
46 server.listen(port, () => {
47 console.log(`listening on *:${port}`);