diff --git a/NVRJS.js b/NVRJS.js index 315e50c..64be4d1 100644 --- a/NVRJS.js +++ b/NVRJS.js @@ -596,18 +596,20 @@ function InitCamera(Cam, cameraID) { detached: true, stdio: ['ignore', 'ignore', 'ignore', 'pipe', 'pipe'] }; - const respawn = (Spawned) => { + const respawn = (Spawned,socket=null) => { const MP4F = new MP4Frag(); const IOptions = { path: '/streams/' + cameraID }; - const Socket = io(HTTP, IOptions); - Socket.on('connection', (ClientSocket) => { - if (CheckAuthMW(ClientSocket)) { - ClientSocket.emit('segment', MP4F.initialization); - } - }); + const Socket = socket||io(HTTP, IOptions); // avoid adding a ton of listeners to the same HTTP server + if(!socket) { + Socket.on('connection', (ClientSocket) => { + if (CheckAuthMW(ClientSocket)) { + ClientSocket.emit('segment', MP4F.initialization); + } + }); + } MP4F.on('segment', (data) => { Socket.sockets.sockets.forEach((ClientSocket) => { @@ -623,7 +625,8 @@ function InitCamera(Cam, cameraID) { MP4F.destroy(); setTimeout(() => { respawn( - childprocess.spawn(config.system.ffmpegLocation, CommandArgs, Options) + childprocess.spawn(config.system.ffmpegLocation, CommandArgs, Options), + Socket ); }, 10000); });