Closed xabberd closed 4 years ago
my wss server working but i dont get frames with jsmpeg and html
Unfortunately this module isn't designed to use wss right now. If this grows as a request I will look into adding it. It would be great to have, but for now I'm not sure I can dig into it. Thanks for the suggestion though!
Sorry for my late answer. I have use your code for wss and now its work with jsmpeg. Thanks for support. :)
So does this mean this will only still work on http sites? Sorry for the confusion but I would love to learn how to get it going on https.
Figured it out hopefully it can help another! The issue was solved using a wss with a slight modification.
Inspired by this
Sample index.js for node
var key = fs.readFileSync('../yoursite_com.key');
var cert = fs.readFileSync( '../yoursite_com.crt' );
var ca = fs.readFileSync( '../yoursite_com.csr' );
global.options = {
key: key,
cert: cert,
ca: ca
};
const app = express();
//Some routes here yada yada
var serverH = https.createServer(options, app);
//On a secure websocket request from the front end, this gets hit
serverH.on('upgrade', function upgrade(request, socket, head) {
const pathname = url.parse(request.url).pathname;
//Check to make sure it is infact a correct wss request
if (pathname.includes('/socketSecure/'))
{
console.log("path :" + pathname)
var splitPath = pathname.split('/');
//Here I am parsing the request path to a UID that is going to help me get the ws connection
//I have a dictionary of these active ws connections
var reqKey = splitPath[2]+ splitPath[3];
for (var key in ClientSteams) {
// check if the property/key is defined
if (ClientSteams.hasOwnProperty(key) && key == reqKey ) {
//This is the key, it finds the wsServer and then emits it to my current connection
ClientSteams[key].wsServer.handleUpgrade(request, socket, head, function done(ws) {
ClientSteams[key].wsServer.emit('connection', ws, request);
});
}}
} else {
socket.destroy();
}
});
Also changed it up a little on the videoStream.js as well, what this is doing is making a Websocket Server without a port
VideoStream.prototype.pipeStreamToSocketServer = function() {
this.wsServer = new ws.Server({
noServer: true
})
With this approach, I can have many SECURE ws connections without getting any server errors.
On express route I have an express route that creates this ClientStreams in this way
async function StartRtspConnection(data, res){
//Some code here
//Yada Yada
var stream = new Stream({
name: 'camera-' + data.customerID + data.locationID,
streamUrl: url
});
ClientSteams[data.customerID + data.locationID] = stream;
res.status(200).end();
}
@alphabin Thanks so much! This is great! Closing issue for now.
I'm still having some major issues getting this to work. The provided instructions detailed aren't really clear on where things are supposed to go. I've generated certs but cannot get the first reply to function, the server just instantly closes.
I am facing error when I try to change the RTSP URL dynamically in node-rtsp-stream, can anyone help me to fix this.
my error is look like this:
TypeError: stream is not a constructor
at D:\Ayonix_Dev_New\ayonix-access-control-nodejs\routes\RTSPLinks.js:48:14
at Layer.handle [as handle_request] (D:\Ayonix_Dev_New\ayonix-access-control-nodejs\node_modules\express\lib\router\layer.js:95:5)
at next (D:\Ayonix_Dev_New\ayonix-access-control-nodejs\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (D:\Ayonix_Dev_New\ayonix-access-control-nodejs\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (D:\Ayonix_Dev_New\ayonix-access-control-nodejs\node_modules\express\lib\router\layer.js:95:5)
at D:\Ayonix_Dev_New\ayonix-access-control-nodejs\node_modules\express\lib\router\index.js:281:22
at Function.process_params (D:\Ayonix_Dev_New\ayonix-access-control-nodejs\node_modules\express\lib\router\index.js:335:12)
at next (D:\Ayonix_Dev_New\ayonix-access-control-nodejs\node_modules\express\lib\router\index.js:275:10)
at Function.handle (D:\Ayonix_Dev_New\ayonix-access-control-nodejs\node_modules\express\lib\router\index.js:174:3)
at router (D:\Ayonix_Dev_New\ayonix-access-control-nodejs\node_modules\express\lib\router\index.js:47:12)
and my post API is :
RTSPRouter.post('/getPreview', (req, res) => {
console.log("Inside stream>>>>", stream.JSON);
// stream.mpeg1Muxer.kill();
stream = new stream({
name: 'name',
streamUrl: req.body.RTSPURL,
wsPort: 9999,
ffmpegOptions: {
'-r': 30
}
})
// stream.mpeg1Muxer.kill()
`res.send(stream)`
})
Look in videoStream.js and do the following.
const server = https.createServer({ cert: fs.readFileSync('./cert/backend.foobar.com.public.pem'), key: fs.readFileSync('./cert/backend.foobar.com.private.pem'), }, function (req, res) { console.log(new Date() + ' ' + req.connection.remoteAddress + ' ' + req.method + ' ' + req.url); res.writeHead(200); res.end("hello foobarbackend\n"); });
this.wsServer = new ws.Server({ server })
You also have to adapt the VideoStream.prototype.stop Add i line where you stop the http server. this.wsServer.close() this.httpsserver.close(function () { console.log('HTTPS Server closed!'); }); this.stream.kill()