summary |
shortlog |
log |
commit | commitdiff |
tree
raw |
patch |
inline | side by side (from parent 1:
cd5180d)
The server saves every message it receives, and replays them to each
new client when it joins.
Note: The server is only holding messages in memory, so there's not
yet any persistence across server restarts.
const io = new Server(server);
const port = 2122;
const io = new Server(server);
const port = 2122;
app.get('/index.html', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
io.on('connection', (socket) => {
app.get('/index.html', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
io.on('connection', (socket) => {
- socket.on('comment', (msg) => {
- io.emit('comment', msg);
+ // Replay old comments to a newly-joining client
+ comments.forEach((comment) => {
+ socket.emit('comment', comment)
+ });
+ // When any client comments, send that to all clients (including sender)
+ socket.on('comment', (comment) => {
+ io.emit('comment', comment);
+ comments.push(comment);