Last active
December 18, 2015 22:59
-
-
Save shimondoodkin/5858496 to your computer and use it in GitHub Desktop.
realtime console in nodejs.
node.js enables to integrate tooling into the application.
for a quick tab refresh to build front end tooling.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//functions.js | |
//f.rtconsole('generate.py build','/mnt/hda1/www/bursa/public/charts/generate.py',['build'],req,res,purl) | |
var tempstreams={}; | |
function rtconsole(streamname,command,command_args,req,res,purl) | |
{ | |
//var streamname='genbuild'; | |
//if(global.tempstreams===undefined)tempstreams={}; // ceate some global to store text and status | |
if(tempstreams[streamname]===undefined)tempstreams[streamname]={}; | |
var tempstream=tempstreams[streamname]; | |
if(tempstream.text===undefined)tempstream.text=""; | |
if(tempstream.working===undefined)tempstream.working=false; | |
if(tempstream.busy===undefined)tempstream.busy=false; | |
res.writeHead(200, | |
{ | |
'Cache-Control': 'no-cache', | |
'Pragma': 'no-cache', | |
'Content-Type': 'text/html', | |
'Expires':"Tue, 01 Jan 2000 12:12:12 GMT" | |
}); | |
var spawn = require('child_process').spawn | |
if(!tempstream.working && !tempstream.busy) | |
{ | |
f.log('rtconsole running: '+streamname); | |
tempstream.working=true; | |
tempstream.text=""; | |
var runcommand = spawn(command,command_args) | |
runcommand.stdout.on('data', function (data) { if(data[0]==8) return; tempstream.text+=data.toString('utf-8')+'\r\n'; }); | |
runcommand.stderr.on('data', function (data) { if(data[0]==8) return; tempstream.text+=data.toString('utf-8')+'\r\n'; }); | |
runcommand.on('close', function (code) { tempstream.text+=':-)'; tempstream.busy=true; tempstream.working=false; setTimeout(function(){tempstream.busy=false;},1500); }); | |
} | |
// if(tempstream.working) res.write('<script>setTimeout(function(){location.reload(false)},500)</script>'); | |
if(tempstream.working&&!purl.query.json) | |
res.write('<script>'+ | |
'function requ()'+'\r\n'+ | |
'{'+'\r\n'+ | |
' var xhr = new XMLHttpRequest(); '+'\r\n'+ | |
' xhr.open("GET", location.href+"?json=1", true);'+'\r\n'+ | |
' xhr.onreadystatechange = function()'+'\r\n'+ | |
' {'+'\r\n'+ | |
' if (xhr.readyState == 4)'+'\r\n'+ | |
' {'+'\r\n'+ | |
' var resp={}; '+'\r\n'+ | |
' try{resp=JSON.parse(xhr.responseText)}catch(e){}'+'\r\n'+ | |
' document.getElementById("resp").innerHTML = resp.text;'+'\r\n'+ | |
' if(resp.working)setTimeout(requ,500);'+'\r\n'+ | |
' }'+'\r\n'+ | |
' };'+'\r\n'+ | |
' xhr.send();'+'\r\n'+ | |
'}'+'\r\n'+ | |
'requ();'+'\r\n'+ | |
'</script>'); | |
res.end(purl.query.json?JSON.stringify(tempstream):'<pre id="resp">\r\n'+tempstream.text+'</pre>'); | |
} | |
exports.rtconsole=rtconsole; | |
var f=require('functions.js') | |
if(purl.pathname=='/gensource') | |
{ | |
f.rtconsole('generate.py source','/mnt/hda1/www/bursa/public/charts/generate.py',['source'],req,res,purl); | |
} | |
else if(purl.pathname=='/genbuild') | |
{ | |
f.rtconsole('generate.py build','/mnt/hda1/www/bursa/public/charts/generate.py',['build'],req,res,purl) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment