Last active
August 29, 2015 14:16
-
-
Save Shu-Ji/a27d7b274263719981ae to your computer and use it in GitHub Desktop.
This file contains 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
<!DOCTYPE html> | |
<html > | |
<head> | |
<meta charset="utf-8"> | |
</head> | |
<body> | |
<script> | |
var KILL_FLAG = 'killnwdemo-sanhaolu.com'; | |
var MAX_PORT = 65535; | |
var port = 9527; | |
var child_process = require('child_process'); | |
var exec = child_process.exec; | |
var execSync = child_process.execSync; | |
var http = require('http'); | |
var win = require('nw.gui').Window.get(); | |
// win.maximize(); | |
win.hide(); | |
function killAllPaboTornadoServer(){ | |
cmd = "ps aux | grep " + KILL_FLAG + " | awk '{print $2}' | xargs kill"; | |
execSync(cmd); | |
} | |
win.on('close', function(){ | |
this.hide(); | |
// kill all started tornado servers. | |
killAllPaboTornadoServer(); | |
this.close(1); | |
}); | |
function pingAgainTornadoServer(){ | |
if(port > MAX_PORT){ | |
return; | |
} | |
var cmd = 'python app.py --killnwdemo=' + KILL_FLAG + ' --port=' + port; | |
exec(cmd, function(error, stdout, stderr){ | |
if(error){ | |
port += 1; | |
setTimeout(function(){ | |
pingAgainTornadoServer(); | |
}, 1); | |
} | |
}); | |
} | |
killAllPaboTornadoServer(); | |
pingAgainTornadoServer(); | |
function ping(){ | |
function pingAgain(){ | |
setTimeout(function(){ | |
ping(); | |
}, 1); | |
} | |
var options = { | |
hostname: '127.0.0.1', | |
port: port, | |
path: '/nwdemo/ping', | |
method: 'GET' | |
}; | |
var req = http.request(options, function(res){ | |
res.on('data', function (chunk) { | |
if(chunk == '/nwdemo/pong'){ | |
location.href = 'http://127.0.0.1:' + port; | |
// win.showDevTools(); | |
win.show(); | |
}else{ | |
pingAgain(); | |
} | |
}); | |
}); | |
req.on('error', function(e){ | |
pingAgain(); | |
}); | |
setTimeout(function(){ | |
req.abort(); | |
}, 1000); | |
req.end(); | |
} | |
ping(); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment