view www/m/js/nodescore-client.js @ 24:22f1c38007ff

latency ping pong check added for clients
author tzara <rc-web@kiben.net>
date Wed, 11 Jul 2012 19:44:37 +0100
parents e1a02869da08
children ac9641ecf84f
line wrap: on
line source
/////////////////////////////////////////////////
// connect to websocket

var socket = io.connect();

//socket.on("metroPulse", metronomeTick);
/////////////////////////////////////////////////
socket.on("metroPulse", pulseInClient);
function pulseInClient(pulse,groupID,metrobeat){
    var groupPage=document.getElementById('group').value
    if (groupID == groupPage) {
	metronomeTick(1000, groupID, metrobeat);
    }
}
/////////////////////////////////////////////////
function  metroCss(beat, beatcolor,text){
    var color = beatcolor;
    
    $(".metrocase > div").each(function(){$(this).stop()});
    $(".metrocase > div").each(function(){$(this).css('background-color', beatcolor)});
    $(".metrocase > div").each(function(){$(this).text(" ")});
    $(".metrocase > div").each(function(){$(this).animate({opacity: 0.6},
							  50,
							  function() { $(this).animate({opacity:0.0}); }
							 );
					 })  
	}

function metronomeTick(pulse, voice,metrobeat) {
    var color = "gray"
    metroCss(0, "red", "4")    
};

/////////////////////////////////////////////////

// update the stopwatch value on the client page in line with server
socket.on("chronFromServer", function(chron) {
    $("div#client_chronometer").text(chron);
});


// server time

//var groupPage=document.getElementById('group').value
//if (groupID == groupPage) {
    socket.on("dateTime", function(datetime) {
//	console.log(datetime)
	$("div#datetime").text("Server Time: " +datetime);
	
    });


/////////////////////////////////////////////////
// countdown to change

socket.on("countinFromServer", countinClient);
function countinClient(groupID, currentseconds,mm,text,colour,background){
    var groupPage=document.getElementById('group').value
    if (groupID == groupPage) {
	//console.log(currentseconds);
	document.getElementById("countinnumber").style.visibility="visible";
	document.getElementById("countinnumber").style.visibility="visible";
	//$("#countin").text(text);
	$("#countinnumber").text(currentseconds);
	$("#countinnumber").css('background-color', background);
	
	document.getElementById("countinnumber").style.color=colour;

	if ( currentseconds == 0) {
	    document.getElementById("countinnumber").style.visibility='hidden';
//	    document.getElementById("countin").style.visibility='hidden';
	}
    }}

/////////////////////////////////////////////////

function pageFlip(unit) {
    console.log("flipping page:"+ unit);
    $('#sections').trigger('goto', [parseFloat(unit)]); 
//    $('#sections-preview').trigger('goto', [parseFloat(unit+1)]); 
    //metroStart(1000); 
    //document.getElementById("countdowncase").style.visibility="hidden";
}



/////////////////////////////////////////////////

// call the fancy jquery functions

function slideTo (target) { $('#sections').trigger('goto', [target]); }
function pad2(number) { return (number < 10 ? '0' : '') + number }       

/////////////////////////////////////////////////

socket.on("pageFlipfromserver", pageTurn);
function pageTurn (group,unit,time,mm) {
    var groupPage=document.getElementById('group').value;
    //console.log("fromservercommand has been executed on client");
    if (group == groupPage) {
	var g= pad2(group); 
	var p= pad2(unit);
//	pageFlip(unit); 
	pageTurnB(unit)
 
   }
    else { 
	console.log("not for this group... ignoring... for group:" + group );
    }
}

function pageTurnB(unit) {
    var units=6;
    var section = "seta/"
    var next=(((unit+1)%units)+units)%units
    console.log("HOP TURN" + unit+ "next:" + next);
//    $("#live").html($("#unit"+unit).html());
    $("#live").load(section+"music.html #"+unit +" *");
 
    $("#preview").html('<img src=' + section+"img/thumbs/"+next + ".png" + '>');
}

////////////////////////////////////////////////

/* 

keyboard controls 
+++++++++++++++++++++++++

SPACE to toggle visibilty of preview
m to toggle visibilty of metronome
s to toggle visibilty of stopwatch
h to hide all above

*/

function toggle_visibility(id) {
    var e = document.getElementById(id);
    if(e.style.display == 'block')
        e.style.display = 'none';
       else
           e.style.display = 'block';
}

$(document).keypress(function(e){
    // "space  bar" for next unit preview
    var checkWebkitandIE=(e.which==32 ? 1 : 0);
    var checkMoz=(e.which==32 ? 1 : 0);
    // "m" button for metronome
    var mcheckWebkitandIE=(e.which==109 ? 1 : 0);
    var mcheckMoz=(e.which==109 ? 1 : 0);
    // "s" button for stopwatch/chronometer
    var ccheckWebkitandIE=(e.which==115 ? 1 : 0);
    var ccheckMoz=(e.which==115 ? 1 : 0);

  // "h" button for hideall
    var hcheckWebkitandIE=(e.which==104 ? 1 : 0);
    var hcheckMoz=(e.which==104 ? 1 : 0);

//    console.log(e.which);
    if (checkWebkitandIE || checkMoz) { toggle_visibility('preview') }   
    if (mcheckWebkitandIE || mcheckMoz) { toggle_visibility('metro') }   
    if (ccheckWebkitandIE || ccheckMoz) { toggle_visibility('client_chronometer') }   
    if (hcheckWebkitandIE || hcheckMoz) { 
	toggle_visibility('client_chronometer') 
	toggle_visibility('metro') 
	toggle_visibility('preview') 
}
    });

////////////////////////////////////////////////
// this needs to have a variable to define the websocket
// otherwise we will pings from all sockets connected
// no! the server broadcasts the ping and the clients emit the pong!
// ah but then the time reported back from the server needs to be targeted
// to specific client..
//////////////////////////////////////////////
// Latency "Pong"

socket.on("timeFromServer", function(n) { 
    socket.emit("clientTimeResponse",n);
});
socket.on("latencyFromServer", function(latency) {
    $("#client_latency").text("Latency: "+latency+"ms.")
    //console.log(latency)
});

function getLatencies(x) { socket.emit("getLatencies", x); }

//////////////////////////////////////////////