-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
79 lines (63 loc) · 2.09 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const express = require('express')
const app = express()
const http = require('http').createServer(app)
const {Server} = require('socket.io')
const path = require("path")
const io = new Server(http, {
path: '/socket'
})
const {addUser, getUsers, deleteUser, addVoteToUser, restartVotes} = require('./models/users')
io.on('connection', (socket) => {
console.log('A Client connected')
socket.on('create', ({roomID}, callback) => {
socket.join(roomID)
callback()
})
socket.on('checkRoom', ({roomID}, callback) => {
//Search if rooms exists
if(!io.sockets.adapter.rooms.get(roomID)){return callback("Room doesn't exist")}
callback()
})
socket.on('join', ({roomID, name}, callback) => {
const {user, error} = addUser(socket.id, name, roomID)
if (error) return callback(error)
socket.join(user.roomID)
io.in(user.roomID).emit('users', getUsers(user.roomID))
callback()
})
socket.on('vote', score => {
const user = addVoteToUser(socket.id, score)
io.in(user.roomID).emit('users', getUsers(user.roomID))
return
})
socket.on('restartVotes', roomID => {
restartVotes(roomID)
io.in(roomID).emit('users', getUsers(roomID))
return
})
socket.on('setShowResult', ({roomID}) => {
io.in(roomID).emit('showResult', true)
return
})
socket.on('disconnect', () => {
console.log('A Client disconnected with ID:', socket.id)
const deletedUser = deleteUser(socket.id)
console.log('Deleted user', deletedUser)
if (deletedUser) return io.in(deletedUser.roomID).emit('users', getUsers(deletedUser.roomID))
})
})
app.get('/api', (req, res) => {
res.send({message: 'hola'})
})
if(process.env.NODE_ENV === 'production'){
//Express will serve up production assets
//like our main.js or main.css
app.use(express.static('client/build'));
//Express will serve up the index.html file
//if it doesnt recognize the route
app.get('*', (req,res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
}
const PORT = process.env.PORT || 5000
http.listen(PORT, () => console.log('Listening'));