-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
34 lines (26 loc) · 943 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 express = require('express')
const path = require('path')
const app = express()
const PORT = process.env.PORT || 4000
const server = app.listen(PORT, () => console.log(`💬 server on port ${PORT}`))
const io = require('socket.io')(server)
app.use(express.static(path.join(__dirname, 'public')))
let socketsConected = new Set()
io.on('connection', onConnected)
function onConnected(socket) {
console.log('Socket connected', socket.id)
socketsConected.add(socket.id)
io.emit('clients-total', socketsConected.size)
socket.on('disconnect', () => {
console.log('Socket disconnected', socket.id)
socketsConected.delete(socket.id)
io.emit('clients-total', socketsConected.size)
})
socket.on('message', (data) => {
// console.log(data)
socket.broadcast.emit('chat-message', data)
})
socket.on('feedback', (data) => {
socket.broadcast.emit('feedback', data)
})
}