diff --git a/libs/dataPort.js b/libs/dataPort.js index 24d66b95..e7fad4e4 100644 --- a/libs/dataPort.js +++ b/libs/dataPort.js @@ -52,27 +52,16 @@ module.exports = function(s,config,lang,app,io){ }) } client.on('message', onAuthenticate) + client.on('close', () => { + clearTimeout(client.killTimer) + }) + client.on('disconnect', () => { + clearTimeout(client.killTimer) + }) + }) + s.onHttpRequestUpgrade('/dataPort',(request, socket, head) => { + theWebSocket.handleUpgrade(request, socket, head, function done(ws) { + theWebSocket.emit('connection', ws, request) + }) }) - theWebSocket.broadcast = function(data) { - theWebSocket.clients.forEach((client) => { - try{ - client.sendData(data) - }catch(err){ - // console.log(err) - } - }) - }; - - s.httpServer.on('upgrade', function upgrade(request, socket, head) { - const pathname = url.parse(request.url).pathname; - if (pathname === '/dataPort') { - theWebSocket.handleUpgrade(request, socket, head, function done(ws) { - theWebSocket.emit('connection', ws, request) - }) - } else if (pathname.indexOf('/socket.io') > -1) { - return; - } else { - socket.destroy(); - } - }); } diff --git a/libs/extenders.js b/libs/extenders.js index 275c4e71..49128aa4 100644 --- a/libs/extenders.js +++ b/libs/extenders.js @@ -189,6 +189,11 @@ module.exports = function(s,config){ s.onDataPortMessageExtensions.push(callback) } // + s.onHttpRequestUpgradeExtensions = {} + s.onHttpRequestUpgrade = function(nameOfCallback,callback){ + s.onHttpRequestUpgradeExtensions[nameOfCallback] = callback + } + // /////// VIDEOS //////// s.insertCompletedVideoExtensions = [] s.insertCompletedVideoExtender = function(callback){ diff --git a/libs/monitor.js b/libs/monitor.js index 05769e00..b2906d4f 100644 --- a/libs/monitor.js +++ b/libs/monitor.js @@ -931,10 +931,10 @@ module.exports = function(s,config,lang){ onDetectorJpegOutputSecondary(e,data) }) }else{ - s.group[e.ke].activeMonitors[e.id].spawn.stdio[4].on('data',function(data){ + s.group[e.ke].activeMonitors[e.id].spawn.stdio[4].on('data',function(data){ onDetectorJpegOutputAlone(e,data) }) - } + } }else if(e.details.detector_use_detect_object === '1' && e.details.detector_send_frames !== '1'){ s.group[e.ke].activeMonitors[e.id].spawn.stdio[4].on('data',function(data){ onDetectorJpegOutputSecondary(e,data) diff --git a/libs/webServer.js b/libs/webServer.js index 21f3c0df..dbea624d 100644 --- a/libs/webServer.js +++ b/libs/webServer.js @@ -1,8 +1,9 @@ -var fs = require('fs'); -var http = require('http'); -var https = require('https'); -var express = require('express'); -var app = express() +const fs = require('fs'); +const url = require('url'); +const http = require('http'); +const https = require('https'); +const express = require('express'); +const app = express() module.exports = function(s,config,lang,io){ //get page URL if(!config.baseURL){ @@ -103,11 +104,21 @@ module.exports = function(s,config,lang,io){ }) } //start HTTP + const onHttpRequestUpgradeExtensions = s.onHttpRequestUpgradeExtensions; var server = http.createServer(app); server.listen(config.port,config.bindip,function(){ console.log(lang.Shinobi+' : Web Server Listening on '+config.port); }); - s.httpServer = server; + server.on('upgrade', function upgrade(request, socket, head) { + const pathname = url.parse(request.url).pathname; + if(typeof onHttpRequestUpgradeExtensions[pathname] === 'function'){ + onHttpRequestUpgradeExtensions[pathname](request, socket, head) + } else if (pathname.indexOf('/socket.io') > -1) { + return; + } else { + socket.destroy(); + } + }); if(config.webPaths.home !== '/'){ io.attach(server,{ path:'/socket.io',