forked from bufferapp/buzzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
50 lines (40 loc) · 1.18 KB
/
index.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const http = require('http')
const express = require('express')
const socketio = require('socket.io')
const app = express();
const server = http.Server(app);
const io = socketio(server);
const title = 'Buffer Buzzer'
let data = {
users: new Set(),
buzzes: new Set(),
}
const getData = () => ({
users: [...data.users],
buzzes: [...data.buzzes].map(b => {
const [ name, team ] = b.split('-')
return { name, team }
})
})
app.use(express.static('public'))
app.set('view engine', 'pug')
app.get('/', (req, res) => res.render('index', { title }))
app.get('/host', (req, res) => res.render('host', Object.assign({ title }, getData())))
io.on('connection', (socket) => {
socket.on('join', (user) => {
data.users.add(user.id)
io.emit('active', [...data.users].length)
console.log(`${user.name} joined!`)
})
socket.on('buzz', (user) => {
data.buzzes.add(`${user.name}-${user.team}`)
io.emit('buzzes', [...data.buzzes])
console.log(`${user.name} buzzed in!`)
})
socket.on('clear', () => {
data.buzzes = new Set()
io.emit('buzzes', [...data.buzzes])
console.log(`Clear buzzes`)
})
})
server.listen(8090, () => console.log('Listening on 8090'))