rc-web@21: /* rc-web@0: //////////////////////////////////////////// rc-web@21: rc-web@21: nodescore server rc-web@21: nodescore.kiben.net rc-web@21: nodescore@kiben.net rc-web@21: rc-web@0: //////////////////////////////////////////// rc-web@21: */ rc-web@49: rc-web@0: var sio = require('socket.io') rc-web@0: , http = require('http') root@53: , ch = require('chronometer') rc-web@0: , fs = require('fs') rc-web@0: , static = require('node-static'); rc-web@49: rc-web@54: var requirejs = require('requirejs'); rc-web@54: rc-web@54: requirejs.config({ rc-web@54: //Pass the top-level main.js/index.js require rc-web@54: //function to requirejs so that node modules rc-web@54: //are loaded relative to the top-level JS file. rc-web@54: nodeRequire: require, rc-web@54: findNestedDependencies: true rc-web@54: rc-web@54: }); rc-web@54: rc-web@43: // run webserver serving static html rc-web@43: //////////////////////////////////////////// root@53: var clientFiles = new static.Server('/usr/share/nodescore/www'); rc-web@43: var httpServer = http.createServer( rc-web@43: function(request, response) { rc-web@43: request.addListener('end', function () { rc-web@43: clientFiles.serve(request, response); rc-web@43: process.setMaxListeners(0); rc-web@43: }); rc-web@43: }); rc-web@43: rc-web@58: httpServer.listen(8890); rc-web@43: rc-web@24: var pinging=0 rc-web@42: console.log("ping set to 0") rc-web@21: rc-web@54: //requirejs(['socketsstuff'],function(socketsstuff) {}); rc-web@0: //////////////////////////////////////////// rc-web@0: // connect to websockets rc-web@0: //////////////////////////////////////////// rc-web@0: rc-web@49: io = sio.listen(httpServer) rc-web@0: , nicknames = {}; rc-web@0: rc-web@49: //var sequencer = require('./sequencer') rc-web@42: rc-web@49: io.set('log level', 1); // reduce logging rc-web@0: io.sockets.on('connection', function (socket) { rc-web@0: rc-web@42: socket.on('nickname', function (nick, fn) { rc-web@42: if (nicknames[nick]) { rc-web@42: fn(true); rc-web@42: } else { rc-web@42: fn(false); rc-web@42: nicknames[nick] = socket.nickname = nick; rc-web@42: socket.broadcast.emit('announcement', nick + ' connected'); rc-web@42: io.sockets.emit('nicknames', nicknames); rc-web@42: } rc-web@42: }); rc-web@38: rc-web@42: /// chat user messages to screens and to log file rc-web@42: // date format for filename rc-web@38: rc-web@42: var da = new Date(); var dtstring = da.getFullYear()+ '-' + da.getMonth()+ '-' + da.getDate(); rc-web@42: rc-web@42: //////////////////////// rc-web@42: /// log messages to file rc-web@42: socket.on('user message', function (msg) { rc-web@42: fs.open('logs/chatlog-'+dtstring+'.txt', 'a', 666, function( e, id ) { rc-web@42: //time format for message stamp rc-web@42: var dt = new Date();var timestring = dt.getHours() + ':' + dt.getMinutes() + ':' + dt.getSeconds(); rc-web@42: rc-web@42: socket.broadcast.emit('user message', socket.nickname, msg); rc-web@42: rc-web@42: var fs = require('fs'), str = msg; rc-web@42: fs.write( id, timestring+" " + socket.nickname + ": "+ msg+"\n", null, 'utf8', function(){}); rc-web@42: }); rc-web@38: }); rc-web@54: rc-web@54: // /* rc-web@0: //////////////////////////////////////////// rc-web@54: // chronometer + sequencer controls (all this should be modularised) rc-web@0: //////////////////////////////////////////// rc-web@0: rc-web@54: socket.on('stopWatch', function (state) { stopWatch(state);}); rc-web@54: socket.on('stopChr', function () { stopChr();}); rc-web@54: socket.on('resetChr', function () { resetChr();}); rc-web@54: socket.on('startSeq', function () { startChr();}); rc-web@54: var chronstate=0; rc-web@0: rc-web@54: // send the date/time every second rc-web@49: rc-web@49: xdatetime = setInterval(function () { rc-web@49: d = ch.xdateTime() rc-web@49: socket.broadcast.emit('dateTime', d) rc-web@54: socket.emit('dateTime', d) rc-web@49: }, 1000) rc-web@54: rc-web@54: rc-web@54: // if not already started start the chronometer and sequencer rc-web@54: function startChr(socket) { if (chronstate !== 1) { rc-web@49: chronstate = 1; rc-web@49: chronCtrl(1,100); rc-web@49: step(seqA);step(seqB); step(seqC); step(seqD); rc-web@54: } rc-web@54: } rc-web@54: rc-web@49: // if not already started start the chronometer rc-web@49: function stopWatch(state) { if (chronstate !== 1) { rc-web@49: if (state==1){ rc-web@49: chronstate = 1; rc-web@49: chronCtrl(1,100);} rc-web@0: } rc-web@54: rc-web@54: // if get 0 then stop the chron rc-web@54: if (state==0){ rc-web@54: chronstate = 0; rc-web@54: clearInterval(xstopwatch); rc-web@54: } rc-web@54: // if get 2 then reset the chron rc-web@54: if (state==2){ rc-web@54: chronstate = 0; rc-web@54: c=ch.zeroChron() rc-web@54: socket.broadcast.emit('chronFromServer', c) rc-web@54: socket.emit('chronFromServer', c) rc-web@54: } rc-web@54: } rc-web@42: rc-web@49: rc-web@54: ////////////////////////////// rc-web@54: // /* this should be moved to its own file chronometer.js rc-web@49: rc-web@49: function chronCtrl (state,interval){ rc-web@49: console.log("=========================== chronstate=" + chronstate) rc-web@49: if (state==1){ root@53: var date = new Date() root@53: var starttime = new Date().getTime() / 1000; root@53: //var interval = 1020 - date.getMilliseconds(); rc-web@49: xstopwatch = setInterval(function () { rc-web@54: var nowtime = new Date().getTime() / 1000; rc-web@54: now = nowtime-starttime rc-web@54: hours = parseInt( now / 3600 ) % 24; rc-web@54: minutes = parseInt( now / 60 ) % 60; rc-web@54: seconds = parseInt(now % 60); rc-web@54: milliseconds = Math.floor((now-seconds)*10)%60; rc-web@54: time = (hours < 10 ? "0" + hours : hours) + ":" + (minutes < 10 ? "0" + minutes : minutes) + ":" + (seconds < 10 ? "0" + seconds : seconds) + "."+milliseconds; rc-web@54: //console.log(time) root@53: socket.broadcast.emit('chronFromServer', time) root@53: socket.emit('chronFromServer', time) rc-web@54: }, 100) rc-web@49: } rc-web@49: if (state==0) { rc-web@49: clearInterval(xstopwatch); rc-web@49: } rc-web@49: } rc-web@54: //*/ rc-web@49: rc-web@49: // stop the chronometer rc-web@54: rc-web@49: function stopChr() {console.log("stop chron................................................") rc-web@49: chronCtrl(0) rc-web@49: chronstate=0 rc-web@49: } rc-web@54: rc-web@49: function pad(number) { return (number < 10 ? '0' : '') + number } rc-web@42: function resetChr() {//clearInterval(); rc-web@49: chronstate = 0; rc-web@49: zecsec = 0; seconds = 0; rc-web@49: mins = 0; hours = 0; rc-web@49: chronstate = 0; rc-web@49: var chron = pad(hours) +":"+pad(mins)+ ':'+ pad(seconds)+ ":"+ zecsec rc-web@49: // send 0.0.0 values to display rc-web@49: socket.broadcast.emit('chronFromServer', chron) rc-web@49: socket.emit('chronFromServer', chron) rc-web@49: } rc-web@49: rc-web@54: rc-web@0: //////////////////////////////////////////// rc-web@54: // magic square sequencer (this should be modularised) rc-web@0: //////////////////////////////////////////// rc-web@54: rc-web@54: // all the variables this sequencer needs are in scoreB.js rc-web@0: rc-web@54: requirejs(['scoreB'],function(scoreB) {}); rc-web@54: rc-web@0: var sequencerState = 0; rc-web@54: var countdowntick = function(seq){ rc-web@0: rc-web@54: // var tempoms = Math.floor(60000/seq.mm) rc-web@43: var tempoms = Math.floor(60000/seq.mm) rc-web@54: rc-web@54: var timemultiplier=1000 rc-web@43: var outcount = 4; var incount=4; rc-web@42: var time = ((seq.durations[seq.counter]+1) *timemultiplier) + 30000 + (outcount*tempoms); rc-web@43: var time = Math.floor(time) rc-web@43: var ztime = time; rc-web@43: var totaltime = Math.floor(time/tempoms) rc-web@42: var unit = seq.units[seq.counter]; rc-web@58: var unitlast = seq.units[seq.counter-1]; rc-web@58: rc-web@54: var voice = seq.voice; rc-web@58: console.log(unit,unitlast) rc-web@42: // initiate first page here rc-web@58: socket.broadcast.emit("pageFlipfromserver", voice, unit, time, seq.mm,seq.counter,unitlast); rc-web@58: socket.emit("pageFlipfromserver", voice, unit, time, seq.mm,seq.counter,unitlast); rc-web@58: console.log("pageFlipfromserver", voice, unit, time, seq.mm,seq.counter, seq.durations[seq.counter]) rc-web@58: rc-web@42: rc-web@42: function sequenCer() { rc-web@54: // console.log(ztime) rc-web@42: if (ztime >= 0 ){ rc-web@42: rc-web@0: // basic unit is still the second/1000ms - change this to tempoms? no i dont think so rc-web@0: // count in and count out rc-web@0: //////////////////////////////////////////// rc-web@0: rc-web@43: var counter = Math.floor(ztime/tempoms) rc-web@43: //console.log(counter) rc-web@42: if (counter >= 0 ){ rc-web@58: socket.broadcast.emit('counterText', voice, unit, counter,seq.counter,unitlast); rc-web@58: socket.emit('counterText', voice, unit, counter,seq.counter,unitlast); rc-web@42: rc-web@58: if (counter <= outcount ) { rc-web@58: socket.broadcast.emit('countinFromServer', voice, counter, "","stop in: ", "red", "transparent",unit); rc-web@42: } rc-web@42: rc-web@42: if (counter > (totaltime)-incount && counter <= totaltime ) { rc-web@58: socket.broadcast.emit('countinFromServer', voice, counter-(totaltime-incount), "","play in: ", "green","transparent",unit); rc-web@58: socket.emit('countinFromServer', voice, counter-(totaltime-incount), "","play in: ", "green","transparent",unit); rc-web@42: } rc-web@42: rc-web@42: if (counter == (totaltime)-incount ) { rc-web@58: socket.broadcast.emit('countinFromServer', voice, "+", "","playing.. ", "green","transparent",unit); rc-web@58: socket.emit('countinFromServer', voice, "+", "","playing.. ", "green","transparent",unit); rc-web@42: } rc-web@42: rc-web@42: // remove displayed number with " " at end of both countin/out rc-web@42: rc-web@42: if (counter == 0 ) { rc-web@58: socket.broadcast.emit('countinFromServer', voice, "", "","", "green","transparent",unit); rc-web@58: socket.broadcast.emit('counterText', voice, seq.counter, "", unit); rc-web@58: socket.emit('counterText', voice, seq.counter, "",unit); rc-web@42: } rc-web@42: } rc-web@42: rc-web@42: // on each beat do: rc-web@42: rc-web@49: rc-web@42: // push out the pulse to metronome rc-web@42: seq.metrobeat = (seq.metrobeat+1)%seq.beatsinbar ; rc-web@54: socket.broadcast.emit('metroPulse', tempoms, voice,seq.metrobeat); rc-web@54: socket.emit('metroPulse', tempoms, voice,seq.metrobeat); rc-web@42: } rc-web@42: rc-web@42: // flip the page rc-web@43: if (counter == 0){ rc-web@42: seq.counter = (seq.counter + 1) % seq.durations.length rc-web@54: socket.broadcast.emit("pageFlipfromserver", voice, unit, time, seq.mm,seq.counter); rc-web@58: console.log("pageFlipfromserver", voice, unit, time, seq.mm,seq.counter, seq.durations[seq.counter]) rc-web@42: //delete tockTock; rc-web@42: step(seq); rc-web@42: } rc-web@42: rc-web@42: // decrement the time rc-web@42: ztime -= tempoms rc-web@42: } rc-web@42: rc-web@42: var pulse = setInterval(sequenCer, tempoms); rc-web@42: rc-web@42: socket.on('stopSeq', function () { rc-web@42: //donaldduck = mickeymouse + 7 rc-web@42: sequenCer.clearInterval rc-web@42: console.log("sequencer stopping...") rc-web@42: // grrr why wont this clearInterval work rc-web@42: sequencerState = 0 rc-web@42: clearInterval(pulse) rc-web@42: stopChr(); rc-web@42: }); rc-web@42: }; rc-web@43: rc-web@39: step = function (seq) { rc-web@39: //clearInterval(seq.boo); rc-web@54: clearInterval(countdowntick); rc-web@39: countdowntick(seq) rc-web@39: sequencerState=1; rc-web@42: }; rc-web@49: rc-web@8: socket.on('resetSeq', function () { rc-web@29: console.log("reset") rc-web@29: resetChr(); rc-web@8: }); rc-web@31: rc-web@29: rc-web@24: //////////////////////////////////////////// rc-web@24: // some latency calculations rc-web@24: /////////////////////////////////////////// rc-web@42: rc-web@24: /* rc-web@24: a ping is periodically broadcast to all connected clients each rc-web@24: connected returns a pong to the server via an "emit" and in turn rc-web@24: the server returns each unique client a report of the latency rc-web@24: via another emit - the emit only sends to the source of the rc-web@24: request, whereas the broadcast.emit.. broadcasts.. ie to all rc-web@24: connected clients rc-web@42: rc-web@24: TODO: smooth range and average out results to remove erratic ping rc-web@24: times. rc-web@24: rc-web@24: TODO: rc-web@24: The result then needs to be used to stagger outgoing messages to rc-web@24: compensate for latency - how much compensation is more connected rc-web@24: to the time that any audio/video feed needs to encode/decode as rc-web@24: the latency of the route from node A to node B is inavoidable?! rc-web@24: so maybe latency is irrelevant in this context - we just need to rc-web@24: stagger signals according to encoding decoding times.. hmmm rc-web@24: */ rc-web@24: rc-web@24: rc-web@43: // periodically broadcast a ping rc-web@24: function serverTime(freq) { rc-web@43: if (pinging==0){ st = setInterval(function() { rc-web@43: var pinging=1; rc-web@43: var d = new Date(); var n = d.getTime(); rc-web@43: socket.emit("timeFromServer", n); rc-web@43: }, 1000); } else console.log("already pinging") rc-web@24: } rc-web@42: // receive the pong calculate the latency and rc-web@24: // return the response to the client rc-web@43: rc-web@24: socket.on("clientTimeResponse", function(x) { rc-web@24: var d = new Date(); var n = d.getTime(); rc-web@24: var latency = (n-x)/2; rc-web@24: //console.log("SERVERTIME:"+x + " LATENCY:" + latency); rc-web@24: socket.emit("latencyFromServer", latency); rc-web@24: }); rc-web@42: rc-web@24: // this is the trigger from the control client to start the process rc-web@24: // maybe remove this and have latency connections constantly running rc-web@42: rc-web@24: /* rc-web@42: socket.on("getLatencies", function(x){ rc-web@42: serverTime(x); rc-web@42: }); rc-web@24: */ rc-web@42: rc-web@24: serverTime(1000); rc-web@42: rc-web@42: socket.on('disconnect', function(client) { rc-web@38: console.log(socket.nickname + " is gone..." ) rc-web@24: clearInterval(st); rc-web@42: rc-web@42: if (!socket.nickname) return; rc-web@42: rc-web@42: delete nicknames[socket.nickname]; rc-web@42: socket.broadcast.emit('announcement', socket.nickname + ' disconnected'); rc-web@42: socket.broadcast.emit('nicknames', nicknames); rc-web@24: }); rc-web@38: rc-web@49: rc-web@44: }); rc-web@54: //}); rc-web@54: //exports.io = io; rc-web@54: //exports.sio = sio; rc-web@54: exports.socket= io.sockets; rc-web@58: exports.httpServer = httpServer;