make http websocket upgrade less hardcoded

montage-api
Moe 2021-11-24 15:40:59 -08:00
parent 440939483b
commit 309abf9758
4 changed files with 35 additions and 30 deletions

View File

@ -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();
}
});
}

View File

@ -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){

View File

@ -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)

View File

@ -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',