bugfixes + full synchro w/o button

This commit is contained in:
Kacper Maj 2021-01-27 22:10:50 +01:00
parent 89ef24c4a2
commit cd8c890af5
3 changed files with 63 additions and 37 deletions

View File

@ -28,7 +28,7 @@ function onYouTubePlayerAPIReady() {
player = new YT.Player('ytplayer', { player = new YT.Player('ytplayer', {
height: '420', height: '420',
width: '860', width: '860',
videoId: 'M7lc1UVf-VE', videoId: '',
}); });
// STATE OF PLAYER // STATE OF PLAYER
@ -62,6 +62,27 @@ socket.on('changeVideo', (videoId) => {
player.loadVideoById(`${videoId}`, 'large'); player.loadVideoById(`${videoId}`, 'large');
}); });
socket.on('welcomeMessage', (object) => {
const string = object.text;
const user = object.username;
const createdAt = moment(object.createdAt).format('HH:mm:ss');
const html = Mustache.render(htmlTemplate[0], {
string,
user,
createdAt,
});
messageContainer.insertAdjacentHTML('beforeend', html);
const time = player.getCurrentTime();
const id = videoIdParse(player.getVideoUrl());
setTimeout(() => {
socket.emit('videoSync', time, id);
}, 700);
autoscroll();
});
socket.on('message', (object) => { socket.on('message', (object) => {
// console.log(object); // console.log(object);
const string = object.text; const string = object.text;
@ -136,23 +157,24 @@ socket.emit('join', { username, room }, (error) => {
}); });
// ####### EVENT LISTENERS ####### // // ####### EVENT LISTENERS ####### //
// SYNC VIDEOS // SYNC VIDEOS BUTTON
syncVideosButton.addEventListener('click', () => {
console.log(player.getVideoUrl());
// If video is the same as starting video // syncVideosButton.addEventListener('click', () => {
if ( // console.log(player.getVideoUrl());
videoIdParse(player.getVideoUrl()) ===
videoIdParse(`https://youtube.com/watch?v=M7lc1UVf-VE`)
)
return alert(
'This is a default video, you cannot synchronize it. \nWrite a request for synchronization in the chat or paste your own link.'
);
const time = player.getCurrentTime(); // // If video is the same as starting video
const id = videoIdParse(player.getVideoUrl()); // if (
socket.emit('videoSync', time, id); // videoIdParse(player.getVideoUrl()) ===
}); // videoIdParse(`https://youtube.com/watch?v=M7lc1UVf-VE`)
// )
// return alert(
// 'This is a default video, you cannot synchronize it. \nWrite a request for synchronization in the chat or paste your own link.'
// );
// const time = player.getCurrentTime();
// const id = videoIdParse(player.getVideoUrl());
// socket.emit('videoSync', time, id);
// });
// Change video // Change video
changeVideoButton.addEventListener('click', () => { changeVideoButton.addEventListener('click', () => {
@ -241,26 +263,26 @@ const videoIdParse = (link) => {
return link.slice(link.indexOf('v=') + 2); return link.slice(link.indexOf('v=') + 2);
} }
}; };
socket.on('joinSynchronization', (ytVideoToSync) => { // socket.on('joinSynchronization', (ytVideoToSync) => {
span.onclick = function () { span.onclick = function () {
modal.style.display = 'none'; modal.style.display = 'none';
chatBox.style.visibility = 'visible'; chatBox.style.visibility = 'visible';
// TUTAJ LOGIKA // TUTAJ LOGIKA
if (!ytVideoToSync) return; // if (!ytVideoToSync) return;
player.loadVideoById(ytVideoToSync); // player.loadVideoById(ytVideoToSync);
}; };
window.onclick = function (event) { window.onclick = function (event) {
if (event.target == modal) { if (event.target == modal) {
modal.style.display = 'none'; modal.style.display = 'none';
chatBox.style.visibility = 'visible'; chatBox.style.visibility = 'visible';
if (!ytVideoToSync) return; // if (!ytVideoToSync) return;
player.loadVideoById(ytVideoToSync); // player.loadVideoById(ytVideoToSync);
} }
}; };
return console.log(ytVideoToSync); // return console.log(ytVideoToSync);
}); // });
// TEMPLATES // TEMPLATES
const sidebarTemplate = `<h2 class="room-title">{{room}}</h2> const sidebarTemplate = `<h2 class="room-title">{{room}}</h2>
@ -284,7 +306,7 @@ const htmlTemplate = [
<span class="message__name"> {{user}} </span> <span class="message__name"> {{user}} </span>
<span class="message__meta"> {{createdAt}} </span> <span class="message__meta"> {{createdAt}} </span>
</p> </p>
<p><a href="{{string}}">{{string}}</a></p> <p><a target=_blank href="{{string}}">{{string}}</a></p>
</div>`, </div>`,
`<div class="message"> `<div class="message">
<p class="welcome-mess">{{string}}</p> <p class="welcome-mess">{{string}}</p>

View File

@ -45,7 +45,11 @@ io.on('connection', (socket) => {
socket.broadcast socket.broadcast
.to(user.room) .to(user.room)
.emit('message', generateMessage(`${user.username} has joined`)); .emit(
'welcomeMessage',
generateMessage(`${user.username} has joined`)
);
io.to(user.room).emit('roomData', { io.to(user.room).emit('roomData', {
room: user.room, room: user.room,
users: getUsersInRoom(user.room), users: getUsersInRoom(user.room),

View File

@ -49,8 +49,8 @@
<button id="sendLocation">Send location</button> <button id="sendLocation">Send location</button>
<hr style="padding: 6px; border: 0 none; height: 5px; color: rgb(233, 233, 233)"> <hr style="padding: 6px; border: 0 none; height: 5px; color: rgb(233, 233, 233)">
<button id="changeVideo">Change video</button> <button id="changeVideo">Change video</button>
<hr style="padding: 6px; border: 0 none; height: 5px; color: rgb(233, 233, 233)"> <!-- <hr style="padding: 6px; border: 0 none; height: 5px; color: rgb(233, 233, 233)">
<button id="syncVideos">Synchronize videos</button> <button id="syncVideos">Synchronize videos</button> -->
</div> </div>
</div> </div>