]> git.cworth.org Git - zombocom-ai/blob - index.js
Add peristence of comments
[zombocom-ai] / index.js
1 const express = require('express');
2 const app = express();
3 const http = require('http');
4 const server = http.createServer(app);
5 const { Server } = require("socket.io");
6 const io = new Server(server);
7 const port = 2122;
8
9 comments = [];
10
11 app.get('/index.html', (req, res) => {
12     res.sendFile(__dirname + '/index.html');
13 });
14
15 io.on('connection', (socket) => {
16     // Replay old comments to a newly-joining client
17     comments.forEach((comment) => {
18         socket.emit('comment', comment)
19     });
20     // When any client comments, send that to all clients (including sender)
21     socket.on('comment', (comment) => {
22         io.emit('comment', comment);
23         comments.push(comment);
24     });
25 });
26
27 server.listen(port, () => {
28     console.log(`listening on *:${port}`);
29 });