-
Notifications
You must be signed in to change notification settings - Fork 25
/
render.js
363 lines (325 loc) · 15.6 KB
/
render.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
const notyf = new Notyf({ duration: 1500, position: { x: 'center', y: 'top' } })
function randomString(length, chars) {
var result = '';
for (var i = length; i > 0; --i) result += chars[Math.floor(Math.random() * chars.length)];
return result;
}
function time(state,username,context){
let hours = parseInt(Math.round(context) / 60 /60, 10);
let minutes = parseInt((context / 60) % 60, 10);
let seconds = Math.round(context) % 60
hours = hours < 10 ? "0" + hours.toString() : hours.toString();
minutes = minutes < 10 ? '0'+ minutes.toString() : minutes.toString()
seconds = seconds < 10 ? '0'+ seconds.toString() : seconds.toString()
let contentString = `${username} ${state} the video at ${minutes}:${seconds}`
if(hours != 0){
contentString = `${username} ${state} the video at ${hours}:${minutes}:${seconds}`
}
return contentString
}
const append = message => {
document.getElementById("messages-box").innerHTML = document.getElementById("messages-box").innerHTML + `<div class="col-12 mt-3" id="message"><span class="username" style="color: ${message.pfp}">${message.name}: </span>${message.content}</div>`
}
function appendData(roomName, roomCode){
append({name: "Local Party", content: "Local Party allows you to watch local videos with your friends synchronously while chatting.", pfp: "#f3dfbf"})
append({name: "Local Party", content: `Welcome to ${roomName}`, pfp: "#f3dfbf"})
append({name: "Local Party", content: `Share the room code (${roomCode}) with others to invite them to the party.`, pfp: "#f3dfbf"})
append({name: "Local Party", content: "They would need to have the same video file with them to join this watch party.", pfp: "#f3dfbf"})
append({name: "Local Party", content: "You can change your username in the settings page.", pfp: "#f3dfbf"})
append({name: "Local Party", content: "Source code for the project is available at https://github.com/sheldor1510/local-party", pfp: "#f3dfbf"})
}
document.getElementById('roomCodeText').addEventListener('click', ()=>{
let text = document.getElementById('roomCodeText').innerHTML
navigator.clipboard.writeText(text).then(()=>{
notyf.success("Copied to clipboard")
})
})
var videoPlayer = document.getElementById("video-player")
let lastcurrentime = 0;
const landingPage = document.getElementById("landing")
const profilePage = document.getElementById("profile")
const createPage = document.getElementById("create")
const joinPage = document.getElementById("join")
const roomPage = document.getElementById("room")
const socket = io.connect("https://local-party.herokuapp.com")
socket.on('connect', function (socket) {
console.log('Connected to the server!');
landingPage.style.display = "block"
});
socket.on('user-joined', data => {
if(data.roomCode == localStorage.getItem("roomCode")){
append({
name: data.name,
content: `${data.name} just popped into the party.`,
pfp: data.pfp
})
document.getElementById("pplinparty").setAttribute("title", `People in party: ${data.members}`)
var toolTipTriggerList = [].slice.call(document.querySelectorAll('[data-toggle="tooltip"]'));
toolTipTriggerList.map(function (tooltipTriggerE1){
return new bootstrap.Tooltip(tooltipTriggerE1)
});
document.getElementById("messages-box").scrollTop = document.getElementById("messages-box").scrollHeight
}
})
socket.on('updateMemberInfo', data => {
if(data.roomCode == localStorage.getItem("roomCode")){
document.getElementById("pplinparty").setAttribute("title", `People in party: ${data.members}`)
var toolTipTriggerList = [].slice.call(document.querySelectorAll('[data-toggle="tooltip"]'));
toolTipTriggerList.map(function (tooltipTriggerE1){
return new bootstrap.Tooltip(tooltipTriggerE1)
});
}
})
socket.on('receive', data => {
append({
name: data.name,
content: data.message,
pfp: data.pfp
})
document.getElementById("messages-box").scrollTop = document.getElementById("messages-box").scrollHeight
})
socket.on('left', data => {
append({
name: 'Local Party',
content: `${data.name} left the party.`,
pfp: '#f3dfbf',
})
document.getElementById("pplinparty").setAttribute("title", `People in party: ${data.members}`)
var toolTipTriggerList = [].slice.call(document.querySelectorAll('[data-toggle="tooltip"]'));
var toolTipList = toolTipTriggerList.map(function (tooltipTriggerE1){
return new bootstrap.Tooltip(tooltipTriggerE1)
});
document.getElementById("messages-box").scrollTop = document.getElementById("messages-box").scrollHeight
})
socket.on('leftdefault', data => {
append({
name: 'Local Party',
content: `${data.name} left the party.`,
pfp: '#f3dfbf',
})
document.getElementById("pplinparty").setAttribute("title", `People in party: ${data.members}`)
var toolTipTriggerList = [].slice.call(document.querySelectorAll('[data-toggle="tooltip"]'));
var toolTipList = toolTipTriggerList.map(function (tooltipTriggerE1){
return new bootstrap.Tooltip(tooltipTriggerE1)
});
document.getElementById("messages-box").scrollTop = document.getElementById("messages-box").scrollHeight
})
socket.on('playerControlUpdate', data => {
if(data.message == "play") {
console.log(data)
videoPlayer.currentTime = data.context
allowEmit = false;
videoPlayer.play()
let content = time("played", data.username, data.context)
append({
name: "Local Party",
content: content,
pfp: "#f3dfbf"
})
document.getElementById("messages-box").scrollTop = document.getElementById("messages-box").scrollHeight
}
if(data.message == "pause") {
console.log(data)
videoPlayer.currentTime = data.context
allowEmit = false;
videoPlayer.pause()
let content = time("paused", data.username, data.context)
append({
name: "Local Party",
content: content,
pfp: "#f3dfbf"
})
document.getElementById("messages-box").scrollTop = document.getElementById("messages-box").scrollHeight
}
})
if(localStorage.getItem("username") == null) {
localStorage.setItem("username", "unknown")
}
const colors = ['#F26E5C', '#FCE060', '#63E683', '#6085FC', '#F52C93','#00FFFF','#800080','#006400','#FF8C00','#4B0082']
function random_item(items) {
return items[Math.floor(Math.random() * items.length)];
}
const color = random_item(colors)
if(localStorage.getItem("pfpUrl") == null) {
localStorage.setItem("pfpUrl", color)
}
document.addEventListener("click", function (e) {
if(e.target.id == "createRoomButton") {
landingPage.style.display = "none"
createPage.style.display = "block"
}
if(e.target.id == "roomCreateButton") {
const roomName = document.getElementById("roomname").value
if(roomName.length == 0) {
console.log("error")
document.getElementById("createRoomText").innerHTML = "Please fill in all the fields"
} else {
if(localStorage.getItem("videoPath") == null || localStorage.getItem("videoSize") == null || document.getElementById("create-username").value.length == 0) {
document.getElementById("createRoomText").innerHTML = "Please fill in all the fields"
} else {
localStorage.setItem("roomName", roomName)
localStorage.setItem("username", document.getElementById("create-username").value)
const roomCode = randomString(5, '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');
document.getElementById("createRoomText").innerHTML = ""
document.getElementById("roomNameText").innerHTML = roomName
document.getElementById("roomCodeText").innerHTML = roomCode
videoPlayer.setAttribute("src", localStorage.getItem("videoPath"))
var myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");
var raw = JSON.stringify({
"roomName": roomName,
"roomCode": roomCode,
"videoSize": localStorage.getItem("videoSize")
});
var requestOptions = {
method: 'POST',
headers: myHeaders,
body: raw,
redirect: 'follow'
};
fetch("https://local-party.herokuapp.com/room/create", requestOptions)
.then( async (result) => {
const resp = await result.json()
if(resp.message == "success") {
var toolTipTriggerList = [].slice.call(document.querySelectorAll('[data-toggle="tooltip"]'));
var toolTipList = toolTipTriggerList.map(function (tooltipTriggerE1){
return new bootstrap.Tooltip(tooltipTriggerE1)
});
localStorage.setItem("roomCode", roomCode)
appendData(roomName, roomCode)
socket.emit('new-user-joined', { name: localStorage.getItem("username"), roomCode: roomCode, pfp: localStorage.getItem("pfpUrl") })
createPage.style.display = "none"
document.title = `Local Party | ${roomName}`
roomPage.style.display = "block"
}
})
.catch(error => console.log('error', error));
}
}
}
if(e.target.id == "joinRoomButton") {
landingPage.style.display = "none"
joinPage.style.display = "block"
}
if(e.target.id == "roomJoinButton") {
const inputRoomCode = document.getElementById("roomCode").value
if(inputRoomCode.length == 0) {
document.getElementById("joinRoomText").innerHTML = "Please fill in all the fields"
} else {
if(localStorage.getItem("videoPath") == null || localStorage.getItem("videoSize") == null || document.getElementById("join-username").value.length == 0) {
document.getElementById("joinRoomText").innerHTML = "Please fill in all the fields"
} else {
var myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");
var raw = JSON.stringify({
"roomCode": inputRoomCode,
"videoSize": localStorage.getItem("videoSize")
});
var requestOptions = {
method: 'POST',
headers: myHeaders,
body: raw,
redirect: 'follow'
};
fetch("https://local-party.herokuapp.com/room/join", requestOptions)
.then( async (result) => {
const resp = await result.json()
if(resp.message != "success") {
document.getElementById("joinRoomText").innerHTML = resp.message
} else {
document.getElementById("joinRoomText").innerHTML = ""
document.getElementById("roomNameText").innerHTML = resp.roomName
document.getElementById("roomCodeText").innerHTML = resp.roomCode
var toolTipTriggerList = [].slice.call(document.querySelectorAll('[data-toggle="tooltip"]'));
var toolTipList = toolTipTriggerList.map(function (tooltipTriggerE1){
return new bootstrap.Tooltip(tooltipTriggerE1)
});
localStorage.setItem("roomCode", inputRoomCode)
localStorage.setItem("username", document.getElementById("join-username").value)
videoPlayer.setAttribute("src", localStorage.getItem("videoPath"))
appendData(resp.roomName, resp.roomCode)
socket.emit('new-user-joined', { name: localStorage.getItem("username"), roomCode: resp.roomCode, pfp: localStorage.getItem("pfpUrl") })
joinPage.style.display = "none"
document.title = `Local Party | ${resp.roomName}`
roomPage.style.display = "block"
}
})
.catch(error => console.log('error', error));
}
}
}
if(e.target.id == "roomLeaveButton") {
videoPlayer.setAttribute("src", "C:\Users\anshu\Desktop\Anshul\Projects\local-party\src\test.mp4")
socket.emit('disconnectUser', { roomCode: localStorage.getItem("roomCode"), name: localStorage.getItem("username") , pfp: localStorage.getItem("pfpUrl") })
location.reload()
}
if(e.target.id == "backButton") {
joinPage.style.display = "none"
createPage.style.display = "none"
landingPage.style.display = "block"
}
})
const form = document.getElementById("send-form")
form.addEventListener('submit', (e) => {
e.preventDefault()
const messageInput = document.getElementById("messageInp").value
if(messageInput.split(" ").join("").length != 0) {
socket.emit('send', messageInput)
append({
name: localStorage.getItem("username"),
content: messageInput,
pfp: localStorage.getItem("pfpUrl")
})
document.getElementById("messageInp").value = ""
document.getElementById("messages-box").scrollTop = document.getElementById("messages-box").scrollHeight
}
})
let allowEmit = true;
videoPlayer.addEventListener('play', videoControlsHandler, false);
videoPlayer.addEventListener('pause', videoControlsHandler, false);
videoPlayer.addEventListener('d', videoControlsHandler, false);
function videoControlsHandler(e) {
if (e.type == 'play') {
if(allowEmit == true){
socket.emit("playerControl", {message: "play", context: videoPlayer.currentTime, roomCode: localStorage.getItem("roomCode")})
let content = time("played","You", videoPlayer.currentTime)
append({
name: "Local Party",
content: content,
pfp: "#f3dfbf"
})
document.getElementById("messages-box").scrollTop = document.getElementById("messages-box").scrollHeight
}
setTimeout(() => {
allowEmit = true
}, 500);
} else if (e.type == 'pause') {
if(allowEmit == true){
socket.emit("playerControl", {message: "pause", context: videoPlayer.currentTime, roomCode: localStorage.getItem("roomCode")})
let content = time("paused","You", videoPlayer.currentTime)
append({
name: "Local Party",
content: content,
pfp: "#f3dfbf"
})
document.getElementById("messages-box").scrollTop = document.getElementById("messages-box").scrollHeight
}
setTimeout(() => {
allowEmit = true
}, 500);
}
}
function onChangeFile() {
const file = document.getElementById("file-id").files[0]
const path = (window.URL || window.webkitURL).createObjectURL(file)
const size = file.size
localStorage.setItem("videoSize", size)
localStorage.setItem("videoPath", path)
}
function onChangeJoinFile() {
const file = document.getElementById("join-file-id").files[0]
const path = (window.URL || window.webkitURL).createObjectURL(file)
const size = file.size
localStorage.setItem("videoSize", size)
localStorage.setItem("videoPath", path)
}