-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
34 lines (29 loc) · 931 Bytes
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const app = require('express')();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const PORT = 3000;
let connectedUsers = [];
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket){
socket.broadcast.emit('justConnected', socket.id);
if (!connectedUsers.includes(socket.id)){
connectedUsers.push(socket.id)
}
io.emit('connectedUsers', connectedUsers)
socket.on('disconnect', function(){
console.log('user disconnected');
connectedUsers = connectedUsers.filter(i => socket.id !== i)
socket.emit('connectedUsers', connectedUsers)
});
socket.on('chat message', function(msg){
socket.broadcast.emit('chat message', msg);
});
socket.on('whoIsTyping', function(name){
socket.broadcast.emit('whoIsTyping', name);
});
});
http.listen(PORT, function(){
console.log(`I'm listening on ${PORT}`);
})