-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathroom-handler.js
275 lines (235 loc) Β· 6.35 KB
/
room-handler.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
const randomstring = require('randomstring')
DEV = true
module.exports = (io, { users, rooms }) => {
function createRoom(userId, callback) {
console.log('createRoom', userId)
console.log('users', users)
// Verifying user
const user = verifyUser(userId)
console.log('createRoom - user verify', user)
if (!user) {
callback(null)
return
}
let roomId = randomstring.generate({ length: 6, charset: 'alphabetic' })
let room = {
id: roomId,
timestamp: new Date().toJSON(),
name: null,
imageLink: null,
started: false,
playing: false,
video: {
added: false,
type: null,
link: null,
thumbnail: null,
title: null,
},
player: {
loaded: false,
playing: false,
time: 0,
speed: 0,
volume: 0,
captions: null,
},
host: {
id: userId,
nickName: user.nickName,
ready: false,
micOff: true,
},
participants: [],
}
rooms.push(room)
// Joining the room
joinRoom({ userId, roomId }, this)
callback(room)
}
function getRoom(roomId, callback) {
console.log('getRoom')
// Getting the room
const room = verifyRoom(roomId)
// Joining socket-io room
this.join(roomId)
callback(room)
}
function joinRoom({ userId, roomId }, socket) {
console.log('joinRoom - ', userId, roomId)
// Verifying incoming data
const user = verifyUser(userId)
const room = verifyRoom(roomId)
console.log(users)
console.log(user, room)
if (!(user && room)) return
// Checking if participant already in the room
// let participant = room.participants.find((p) => p.id === userId)
const newParticipant = {
id: userId,
nickName: user.nickName,
ready: false,
micOff: true,
}
// if (participant) participant = newParticipant
// else
room.participants.push(newParticipant)
// Joining socket-io room
let socketRef
if (this.join) socketRef = this
else socketRef = socket
socketRef.join(roomId)
// Removing user from room on disconnect
socketRef.on('disconnecting', () => {
console.log('disconnecting')
const roomId = Array.from(socketRef.rooms).find((room) => room !== socketRef.id)
const user = users.find((user) => user.socketId === socketRef.id)
// console.log('here', user, roomId)
if (user && roomId) {
console.log('leaving')
const userId = user.id
console.log(roomId, userId)
leaveRoom({ userId, roomId }, true)
}
})
// Room update
io.to(roomId).emit('room:updated', room)
}
function addVideo({ userId, roomId, video }) {
console.log('addVideo')
// Verifying incoming data
const user = verifyUser(userId)
const room = verifyRoom(roomId)
const isHost = checkIfHost(user, room)
if (!(user && room && isHost)) return
// Adding video to room
const { added, type, link, thumbnail, title } = video
room.video = {
added,
type,
link,
thumbnail,
title,
}
if (room.video.type == 'youtube') {
room.name = title
room.imageLink = thumbnail
}
console.log('updating room')
// Pushing changes to participants
io.to(roomId).emit('room:updated', room)
}
function removeVideo({ userId, roomId, video }) {
console.log('removeVideo')
// Verifying incoming data
const user = verifyUser(userId)
const room = verifyRoom(roomId)
const isHost = checkIfHost(user, room)
if (!(user && room && isHost)) return
room.video = {
added: false,
type: null,
link: null,
thumbnail: null,
title: null,
}
// Pushing changes to participants
io.to(roomId).emit('room:updated', room)
}
function startRoom({ userId, roomId, data }) {
console.log('startRoom')
// Verifying incoming data
const user = verifyUser(userId)
const room = verifyRoom(roomId)
const isHost = checkIfHost(user, room)
if (!(user && room && isHost)) return
const { roomName, imageLink } = data
room.name = roomName
room.imageLink = imageLink
room.started = true
room.playing = true
// Pushing changes to participants
io.to(roomId).emit('room:updated', room)
}
function setRoomInfo({ userId, roomId, data }) {
console.log('setRoomInfo')
// Verifying incoming data
const user = verifyUser(userId)
const room = verifyRoom(roomId)
const isHost = checkIfHost(user, room)
if (!(user && room && isHost)) return
const { roomName, imageLink } = data
room.name = roomName
room.imageLink = imageLink
// Pushing changes to participants
io.to(roomId).emit('room:updated', room)
}
function stop({ roomId, userId }) {
console.log('stop')
// Verifying incoming data
const user = verifyUser(userId)
const room = verifyRoom(roomId)
const isHost = checkIfHost(user, room)
if (!(user && room && isHost)) return
room.playing = false
// Pushing changes to participants
io.to(roomId).emit('room:updated', room)
}
function sync({ roomId, userId, data }) {
console.log('sync')
// Verifying incoming data
const user = verifyUser(userId)
const room = verifyRoom(roomId)
const isHost = checkIfHost(user, room)
if (!(user && room)) return
if (isHost) {
const { loaded, playing, time, speed, volume, captions } = data
room.player = {
loaded,
playing,
time,
speed,
volume,
captions,
}
} else {
const host = verifyUser(room.host.id)
if (host) io.to(host.socketId).emit('room:syncrequest')
}
// Pushing changes to participants
io.to(roomId).emit('room:updated', room)
}
function leaveRoom({ roomId, userId }, hostTempLeave) {
console.log('leaveRoom')
// Verifying incoming data
const user = verifyUser(userId)
const room = verifyRoom(roomId)
const isHost = checkIfHost(user, room)
if (!(user && room)) return
if (isHost && !hostTempLeave) room.started = false
else {
const newParticipants = room.participants.filter((p) => p.id != userId)
room.participants = newParticipants
}
// Pushing changes to participants
io.to(roomId).emit('room:updated', room)
}
// Verifying user
function verifyUser(userId) {
if (!userId) return null
const user = users.find((user) => user.id === userId)
return user
}
// Verifying room
function verifyRoom(roomId) {
if (!roomId) return null
const room = rooms.find((room) => room.id === roomId)
return room
}
// Checking if user is the host of the room
function checkIfHost(user, room) {
if (!user || !room) return false
return user.id === room.host.id
}
return { createRoom, getRoom, joinRoom, addVideo, removeVideo, startRoom, setRoomInfo, stop, sync, leaveRoom }
}