http: detect silent failure when port is used but no error is raised (#570, #578)

This commit is contained in:
bordun 2019-12-05 13:25:51 +01:00
parent d74d4f162d
commit 778323191b
2 changed files with 23 additions and 4 deletions

View file

@ -10,7 +10,8 @@ var urlparser = require('url'),
zeroconf = require('./zeroconf'),
appAddresses = settings.read('appAddresses'),
osc = {},
clients = {}
clients = {},
httpCheckTimeout
function httpRoute(req, res) {
@ -59,6 +60,8 @@ function httpRoute(req, res) {
// absolute resolution (session path)
res.sendFile(path.resolve(url))
}
} else if (url.includes('/osc-ping')) {
httpCheck(true)
} else {
res.writeHead(403)
res.end()
@ -71,6 +74,19 @@ function httpRoute(req, res) {
server.listen(settings.read('httpPort'))
http.get(settings.read('appAddresses')[0] + '/osc-ping', ()=>{}).on('error', ()=>{httpCheck(false)})
httpCheckTimeout = setTimeout(()=>{httpCheck(false)}, 5000)
function httpCheck(ok){
if (!httpCheckTimeout) return
clearTimeout(httpCheckTimeout)
httpCheckTimeout = null
if (ok) {
console.log('App available at ' + appAddresses.join(' & '))
} else {
console.error('Error: could not setup http server, maybe try a different port ?')
}
}
zeroconf.publish({
name: settings.read('appName') + (settings.read('instanceName') ? ' (' + settings.read('instanceName') + ')' : ''),
type: 'http',
@ -104,8 +120,6 @@ var bindCallbacks = function(callbacks) {
}
console.log('App available at ' + appAddresses.join(' & '))
module.exports = {
ipc:ipc,
bindCallbacks:bindCallbacks,