Skip to content

Commit

Permalink
Change Video Client as New Sockets Join
Browse files Browse the repository at this point in the history
Video now syncs properly when a new socket joins on a different player
  • Loading branch information
kyle8998 committed Jan 31, 2018
1 parent a164d78 commit 4c703f1
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 36 deletions.
67 changes: 37 additions & 30 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -343,39 +343,46 @@ <h2>About Vynchronize</h2>
// Should only happen when a new socket joins late

// Current issue: changePlayer is called asynchronously when we need this function to wait for it to finish
console.log("currPlayer="+currPlayer)
console.log("playerId="+playerId)
console.log("currPlayer="+currPlayer)
console.log("playerId="+playerId)
// changeSinglePlayer(playerId)
// currPlayer = playerId

// Change the player if necessary
if (currPlayer != playerId){
// This changes the player then recalls sync afterwards on the host
changeSinglePlayer(playerId)
// changeSinglePlayer(roomnum, playerId).then(console.log("currPlayer="+currPlayer))

}
else{
// This syncs the time and state
switch(currPlayer) {
case 0:
player.seekTo(currTime);
// Sync player state
// IF parent player was paused
if (state == -1 || state == 2)
player.pauseVideo();
// If not paused
else
player.playVideo();
break;

case 1:
dailyPlayer.seek(currTime);
if (state) {
console.log("i pausing!")
dailyPlayer.pause()
}
else {
dailyPlayer.play()
}
break;

default:
console.log("Error invalid player id")
switch(currPlayer) {
case 0:
player.seekTo(currTime);
// Sync player state
// IF parent player was paused
if (state == -1 || state == 2)
player.pauseVideo();
// If not paused
else
player.playVideo();
break;

case 1:
dailyPlayer.seek(currTime);
if (state) {
console.log("i pausing!")
dailyPlayer.pause()
}
else {
dailyPlayer.play()
}
break;

default:
console.log("Error invalid player id")
}
}
});
});

// Change video
socket.on('changeVideoClient', function(data) {
Expand Down
1 change: 1 addition & 0 deletions js/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ socket.on('createYoutube', function(data) {

// Create Daily Motion Player
socket.on('createDaily', function(data) {
console.log("i am in create daily")
// player.destroy()
if (currPlayer != 1) {
// var playerIn = document.getElementById("playerArea")
Expand Down
12 changes: 8 additions & 4 deletions js/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,13 @@ function changePlayer(roomnum, playerId) {
}
}

// Change a single player
function changeSinglePlayer(playerId) {
if (playerId != currPlayer) {
console.log("I changed!")
socket.emit('change single player', { playerId: playerId });
}
return new Promise((resolve, reject) => {
if (playerId != currPlayer) {
console.log("I changed!")
socket.emit('change single player', { playerId: playerId });
}
resolve("socket entered change single player function")
})
}
17 changes: 15 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ io.sockets.on('connection', function(socket){

// This changes the room variable to the player id
io.sockets.adapter.rooms['room-'+roomnum].currPlayer = playerId
console.log(io.sockets.adapter.rooms['room-'+socket.roomnum].currPlayer)

});

Expand All @@ -101,14 +102,17 @@ io.sockets.on('connection', function(socket){

switch(playerId) {
case 0:
io.sockets.emit('createYoutube', {});
socket.emit('createYoutube', {});
break;
case 1:
io.sockets.emit('createDaily', {});
socket.emit('createDaily', {});
break;
default:
console.log("Error invalid player id")
}
// After changing the player, resync with the host
host = io.sockets.adapter.rooms['room-'+socket.roomnum].host
socket.broadcast.to(host).emit('getData');
});


Expand Down Expand Up @@ -143,6 +147,7 @@ io.sockets.on('connection', function(socket){
//callback(true);
socket.roomnum = data;
var host = null
var init = false

// Sets default room value to 1
if (socket.roomnum == null || socket.roomnum == "") {
Expand All @@ -159,19 +164,27 @@ io.sockets.on('connection', function(socket){
if (io.sockets.adapter.rooms['room-'+socket.roomnum] === undefined) {
socket.send(socket.id)
host = socket.id
init = true
//console.log(socket.id)
}
else {
console.log(socket.roomnum)
host = io.sockets.adapter.rooms['room-'+socket.roomnum].host
}

// Actually join the room
console.log(socket.username+" connected to room-"+socket.roomnum)
socket.join("room-"+socket.roomnum);

// Sets the host
io.sockets.adapter.rooms['room-'+socket.roomnum].host = host
// Sets the default values when first initializing
if (init){
io.sockets.adapter.rooms['room-'+socket.roomnum].currPlayer = 0
io.sockets.adapter.rooms['room-'+socket.roomnum].currVideo = 'M7lc1UVf-VE'
}

// Gets current video from room variable
var currVideo = io.sockets.adapter.rooms['room-'+socket.roomnum].currVideo
// Change the video to current One
socket.emit('changeVideoClient', { videoId: currVideo });
Expand Down

0 comments on commit 4c703f1

Please sign in to comment.