-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
112 lines (98 loc) · 3.19 KB
/
server.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
const app = require('express')()
const server = require('http').createServer(app)
const io = require('socket.io')(server)
const path = require('path')
const history = require('connect-history-api-fallback')
app.use(history())
app.use(require('express').static(path.join(__dirname, '/dist')))
const roomData = {}
const roomInterval = {}
io.on('connection', socket => {
let userData = {}
socket.on('createRoom', room => {
userData = {
roomId: room.id,
userId: room.viewers[0]
}
roomData[room.id] = room
roomInterval[room.id] = {
interval: null,
time: 0
}
io.emit('updatedData', room)
})
socket.on('joinRoom', ({ roomId, userId }) => {
userData = { roomId, userId }
let room = roomData[roomId]
if (room && Object.keys(room).length > 0) {
if (room.viewers.indexOf(userId) === -1) {
room.viewers.push(userId)
}
room.video.currentTime = roomInterval[roomId].time
}
io.emit('updatedData', room)
})
socket.on('updatePlayerState', state => {
let room = roomData[userData.roomId]
if (room && Object.keys(room).length > 0) {
if (state === 'playing') {
room.video.status = 'playing'
io.emit('updatedData', room)
roomInterval[userData.roomId].interval = setInterval(() => {
roomInterval[userData.roomId].time++
}, 1000)
} else if (state === 'paused') {
if (roomInterval[userData.roomId] && roomInterval[userData.roomId].interval) {
clearInterval(roomInterval[userData.roomId].interval)
}
room.video.status = 'paused'
io.emit('updatedData', room)
}
}
})
socket.on('updateCurrentTime', time => {
roomInterval[userData.roomId].time = time
roomData[userData.roomId].video.currentTime = time
io.emit('updatedData', roomData[userData.roomId])
})
socket.on('trackNewVideo', id => {
clearInterval(roomInterval[userData.roomId].interval)
roomInterval[userData.roomId].time = 0
roomData[userData.roomId].video.id = id
roomData[userData.roomId].video.currentTime = 0
io.emit('updatedData', roomData[userData.roomId])
})
socket.on('addVideoToQueue', item => {
if (roomData[userData.roomId]) {
roomData[userData.roomId].queue.push(item)
io.emit('updatedData', roomData[userData.roomId])
}
})
socket.on('removeVideoFromQueue', (videoId, currentVideoEnded) => {
if (currentVideoEnded) {
if (roomData[userData.roomId].queue[0].id.videoId === videoId) {
roomData[userData.roomId].queue.splice(0, 1)
}
} else {
let index = roomData[userData.roomId].queue.findIndex(video => video.id.videoId === videoId)
roomData[userData.roomId].queue.splice(index, 1)
}
io.emit('updatedData', roomData[userData.roomId])
})
socket.on('disconnect', () => {
let room = roomData[userData.roomId]
if (room && Object.keys(room).length > 0) {
let index = room.viewers.indexOf(userData.userId)
room.viewers.splice(index, 1)
io.emit('updatedData', room)
}
})
})
setInterval(() => {
Object.keys(roomData).forEach(id => {
if (roomData[id].viewers.length === 0) {
delete roomData[id]
}
})
}, 60000)
server.listen(3000)