-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.js
More file actions
23 lines (20 loc) · 810 Bytes
/
Copy pathchat.js
File metadata and controls
23 lines (20 loc) · 810 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const express = require('express');
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
app.get('/', (req, res) => {
res.render('chat.ejs');
});
io.on('connection', (socket) => {
socket.username = 'anonymous'
console.log('someone connected')
socket.on('message', (msg) => io.emit('message', {'user': socket.username, 'message': msg}))
socket.on('join', (username) => {
socket.username = username;
io.emit('message', { user: '🔵', 'message': socket.username + ' joined the server.'});
})
socket.on('disconnect', (username) => {
io.emit('message', { user: '🔴', 'message': socket.username + ' left the server.'});
})
})
const server = http.listen(3000, () => console.log('Listening on port 3000'))