rc@74: ///////////////////////////////////////////////// rc@74: // connect to websocket rob@95: rc@74: var socket = io.connect(); rob@83: function initPage() { socket.emit("initPage"); rob@83: console.log("init.client.log") rob@83: } rob@83: rc@74: //socket.on("metroPulse", metronomeTick); rc@74: ///////////////////////////////////////////////// rob@95: rc@74: socket.on("metroPulse", pulseInClient); rc@74: function pulseInClient(pulse,groupID,metrobeat){ rc@74: var groupPage=document.getElementById('group').value rc@74: if (groupID == groupPage) { rc@74: metronomeTick(1000, groupID, metrobeat); rc@74: } rc@74: } rob@83: rob@95: ////////////////////////////////////////////////// rob@95: // AUDIO SCORE CUES rob@95: rob@95: var audioUnit1 = new Audio('audio/testfile.ogg'); rob@95: var audioUnit49 = new Audio('audio/testfile.ogg'); rob@95: var audioUnit3 = new Audio('audio/testfile.ogg'); rob@95: var audioUnit4 = new Audio('audio/testfile.ogg'); rob@95: rob@95: function playAudio(a) { rob@95: a.play(); rob@95: } rob@95: rob@95: //playAudio(audio_unit_1); rob@95: rob@95: rc@74: ///////////////////////////////////////////////// rc@74: function metroCss(beat, beatcolor,text){ rc@74: var color = beatcolor; rc@74: $(".metrocase > div").each(function(){$(this).stop()}); rc@74: $(".metrocase > div").each(function(){$(this).css('background-color', beatcolor)}); rc@74: $(".metrocase > div").each(function(){$(this).text(" ")}); rob@84: } rc@74: rc@74: function metronomeTick(pulse, voice,metrobeat) { rc@74: var color = "gray" rob@83: metroCss(0, "red", "4") rob@83: setTimeout(function(){metroCss(0, "black", "4")},150); rc@74: }; rc@74: rc@74: ///////////////////////////////////////////////// rc@74: // update the stopwatch value on the client page in line with server rob@83: socket.on("chronFromServer", function(chron) { $("div#client_chronometer").text(chron); }); rc@74: rc@74: // server time rc@74: socket.on("dateTime", function(datetime) { rob@83: $("div#datetime").text("" +datetime); rc@74: }); rc@74: rc@74: ///////////////////////////////////////////////// rob@93: rob@93: // countin (solo part) rob@84: rc@74: socket.on("countinFromServer", countinClient); rob@83: function countinClient(groupID, currentseconds,mm,text,colour,background,unit){ rc@74: var groupPage=document.getElementById('group').value rc@74: if (groupID == groupPage) { rob@83: $("#counttitle").css('color','black'); rc@74: $("#counttitle").text(text); rc@74: $("#count").text(currentseconds); rc@74: document.getElementById("count").style.color=colour; rob@83: }} rc@74: rc@74: ///////////////////////////////////////////////// rob@93: rob@93: // countdown to change (solo) rob@93: rc@74: socket.on("counterText", cText); rc@74: function cText(groupID, currentseconds,text){ rob@83: $("#totalcountdown"+groupID).text(text); rc@74: var groupPage=document.getElementById('group').value rc@74: if (groupID == groupPage) { rob@83: //console.log(text) rc@74: $("#totalcountdown").text(text); rc@74: }} rc@74: rob@93: // when voice is changed in UI grab and update from server rc@74: rob@93: function changeSoloVoice(v) { socket.emit("changeSoloVoice", v); } rob@84: rob@93: // makes sure page is loaded if not it grabs svg etc rc@74: rob@93: socket.on("pageIni", pageIni); rob@93: function pageIni (group,unit,time,mm,seqcounter,nextunit) { rob@93: //console.log(unit + " ---> " + nextunit) rob@93: var soloist = $("#group").attr('value'); //4//$("#group").value; rob@93: if (group==1){ rob@93: var n1 = $("#previewbox-1").html().indexOf("svg"); rob@93: //console.log(n1) rob@93: if (n1 == -1){ rob@93: //console.log("n1 == -1 so load svg:") rob@93: $("#previewbox-1").html("") rob@93: } rob@93: } rob@93: rob@93: if (group==2){ rob@93: var n2 = $("#previewbox-2").html().indexOf("svg"); rob@93: if (n2 == -1){ rob@93: $("#previewbox-2").html("") rob@93: } rob@93: } rob@84: rob@93: if (group==3){ rob@93: var n3 = $("#previewbox-3").html().indexOf("svg"); rob@93: if (n3 == -1){ rob@93: $("#previewbox-3").html("") rob@93: } rob@93: } rob@84: rob@93: if (group==4){ rob@93: var n4 = $("#previewbox-4").html().indexOf("svg"); rob@93: if (n4 == -1){ rob@93: $("#previewbox-4").html("") rob@93: } rob@83: } rob@93: rob@93: rob@93: if (group==soloist) { rob@93: //console.log("INI sooolooooist=: ---------------------- : " + soloist) rob@93: var n = $("#previewbox-solo").html().indexOf("svg"); rob@93: if (n == -1){ rob@93: $("#previewbox-solo").html("") rob@93: $("#previewbox-solo-next").html("") rob@93: } rob@93: } rob@95: rob@95: rob@93: } rob@93: rob@95: rob@93: socket.on("pageFlip", pageFlip); rob@93: rob@93: // some if logic to check if file is already displayed rob@93: // if not load it - client doesnt need to be present at start rob@93: rob@93: function pageFlip (group,unit,time,mm,seqcounter,nextunit) { rob@93: var soloist = $("#group").attr('value'); rob@93: rob@93: if (group==1){ rob@93: $("#previewbox-1").html("") rob@93: } rob@93: rob@93: if (group==2){ rob@93: $("#previewbox-2").html("") rob@93: } rob@93: rob@93: if (group==3){ rob@93: rob@93: $("#previewbox-3").html("") rob@93: } rob@93: rob@93: if (group==4){ rob@93: $("#previewbox-4").html("") rob@93: } rob@93: rob@95: rob@95: // page flip for solo part section rob@93: rob@93: if (group==soloist) { rob@95: rob@95: // changes the svg and preview images rob@93: $("#previewbox-solo").html("") rob@93: $("#previewbox-solo-next").html("") rob@95: rob@95: // check for audio assets to play rob@95: // audio assets should have a value attribute which should be compared with the unit rob@95: // if the unit has an associated audio attribute it should play rob@95: rob@95: if ( window["audioUnit"+unit] ){ rob@95: console.log("audio asset detected for this unit... playing now...") rob@95: playAudio(window["audioUnit"+unit]); rob@95: } else { console.log("no audio for this unit.."); } rc@74: } rob@95: rob@95: // check for effect associated with unit rob@95: // maybe in future we could use the webAudio API? rob@95: rc@74: } rc@74: rc@74: rob@95: rc@74: //////////////////////////////////////////////// rc@74: rc@74: /* commented out for now as interferes with chat rc@74: need to introduce metakey...manyana... not so usefull anyway really... rc@74: rc@74: keyboard controls rc@74: +++++++++++++++++++++++++ rc@74: rc@74: SPACE to toggle visibilty of preview rc@74: m to toggle visibilty of metronome rc@74: s to toggle visibilty of stopwatch rc@74: h to hide all above rc@74: rc@74: rob@83: */ rob@93: rc@74: function toggle_visibility(id) { rc@74: var e = document.getElementById(id); rc@74: if(e.style.display == 'block') rc@74: e.style.display = 'none'; rc@74: else rc@74: e.style.display = 'block'; rc@74: } rc@74: rc@74: $(document).keypress(function(e){ rc@74: // console.log(e.which); rob@90: // "0" button for hide chat rob@90: var hcheckWebkitandIE=(e.which==48 ? 1 : 0); rob@90: var hcheckMoz=(e.which==48 ? 1 : 0); rc@74: if (hcheckWebkitandIE || hcheckMoz) { rob@90: toggle_visibility('comms') rc@74: } rc@74: }); rc@74: rob@83: //*/ rc@74: rc@74: //////////////////////////////////////////////// rc@74: // this needs to have a variable to define the websocket rc@74: // otherwise we will pings from all sockets connected rc@74: // no! the server broadcasts the ping and the clients emit the pong! rc@74: // ah but then the time reported back from the server needs to be targeted rc@74: // to specific client.. rob@93: rc@74: ////////////////////////////////////////////// rc@74: // Latency "Pong" rob@90: rob@83: socket.on("timeFromServer", function(n) { rob@83: socket.emit("clientTimeResponse",n); }); rc@74: rc@74: socket.on("latencyFromServer", function(latency) { rc@74: $("#client_latency").text("Latency: "+latency+"ms.") rc@74: }); rc@74: rc@74: function getLatencies(x) { socket.emit("getLatencies", x); } rc@74: rc@74: ////////////////////////////////////////////// rc@74: rc@74: rc@74: socket.on('connect', function () { rc@74: $('#chat').addClass('connected'); rc@74: }); rc@74: rc@74: socket.on('announcement', function (msg) { rob@90: $('#Lines').append($('

').append($('').text(msg))); rc@74: }); rc@74: rob@91: socket.on('nicknames', function (nicknames) { rc@74: $('#nicknames').empty().append($('Online: ')); rc@74: for (var i in nicknames) { rc@74: $('#nicknames').append($('').text(nicknames[i])); rc@74: } rc@74: }); rc@74: rc@74: socket.on('user message', message); rc@74: socket.on('reconnect', function () { rc@74: $('#lines').remove(); rc@74: message('System', 'Reconnected to the server'); rc@74: }); rc@74: rc@74: socket.on('reconnecting', function () { rc@74: message('System', 'Attempting to re-connect to the server'); rc@74: }); rc@74: rc@74: socket.on('error', function (e) { rc@74: message('System', e ? e : 'A unknown error occurred'); rc@74: }); rc@74: rc@74: function message (from, msg) { rob@83: $('#lines').prepend($('

').prepend($('').text(from), msg)); rc@74: } rc@74: rc@74: // dom manipulation rc@74: $(function () { rc@74: $('#set-nickname').submit(function (ev) { rc@74: socket.emit('nickname', $('#nick').val(), function (set) { rc@74: if (!set) { rc@74: clear(); rc@74: return $('#chat').addClass('nickname-set'); rc@74: } rc@74: $('#nickname-err').css('visibility', 'visible'); rc@74: }); rc@74: return false; rc@74: }); rc@74: rc@74: $('#send-message').submit(function () { rc@74: message('me', $('#message').val()); rc@74: socket.emit('user message', $('#message').val()); rc@74: clear(); rc@74: $('#lines').get(0).scrollTop = 10000000; rc@74: return false; rc@74: }); rc@74: rc@74: function clear () { rc@74: $('#message').val('').focus(); rc@74: }; rob@79: });