Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
websocket-relay.js from JSMpeg slightly modified to use HTTPS instead of HTTP
// Use the websocket-relay to serve a raw MPEG-TS over WebSockets. You can use
// ffmpeg to feed the relay. ffmpeg -> websocket-relay -> browser
// Example:
// node websocket-relay yoursecret 8081 8082
// ffmpeg -i <some input> -f mpegts https://localhost:8081/yoursecret
var fs = require('fs'),
https = require('https'),
WebSocket = require('ws');
if (process.argv.length < 3) {
console.log(
'Usage: \n' +
'node websocket-relay-secure.js <secret> [<stream-port> <websocket-port>]'
);
process.exit();
}
var STREAM_SECRET = process.argv[2],
STREAM_PORT = process.argv[3] || 8081,
WEBSOCKET_PORT = process.argv[4] || 8082,
RECORD_STREAM = false;
var configs = {
key: '/path/to/cert.pem',
cert: '/path/to/key.pem'
};
if (fs.existsSync(configs.key) && fs.existsSync(configs.cert)) {
var configs2 = {
key: fs.readFileSync(configs.key),
cert: fs.readFileSync(configs.cert)
};
} else {
console.log('Private key and/or Certificate not found!');
process.exit();
}
var httpsserver = https.createServer(configs2);
// Websocket Server
var socketServer = new WebSocket.Server({server: httpsserver, perMessageDeflate: false});
socketServer.connectionCount = 0;
socketServer.on('connection', function(socket, upgradeReq) {
socketServer.connectionCount++;
console.log(
'New WebSocket Connection: ',
(upgradeReq || socket.upgradeReq).socket.remoteAddress,
(upgradeReq || socket.upgradeReq).headers['user-agent'],
'('+socketServer.connectionCount+' total)'
);
socket.on('close', function(code, message){
socketServer.connectionCount--;
console.log(
'Disconnected WebSocket ('+socketServer.connectionCount+' total)'
);
});
});
socketServer.broadcast = function(data) {
socketServer.clients.forEach(function each(client) {
if (client.readyState === WebSocket.OPEN) {
client.send(data);
}
});
};
httpsserver.listen(WEBSOCKET_PORT);
// HTTPS Server to accept incomming MPEG-TS Stream from ffmpeg
var streamServer = https.createServer(configs2, function(request, response) {
var params = request.url.substr(1).split('/');
if (params[0] !== STREAM_SECRET) {
console.log(
'Failed Stream Connection: '+ request.socket.remoteAddress + ':' +
request.socket.remotePort + ' - wrong secret.'
);
response.end();
}
response.connection.setTimeout(0);
console.log(
'Stream Connected: ' +
request.socket.remoteAddress + ':' +
request.socket.remotePort
);
request.on('data', function(data){
socketServer.broadcast(data);
if (request.socket.recording) {
request.socket.recording.write(data);
}
});
request.on('end',function(){
console.log('close');
if (request.socket.recording) {
request.socket.recording.close();
}
});
// Record the stream to a local file?
if (RECORD_STREAM) {
var path = 'recordings/' + Date.now() + '.ts';
request.socket.recording = fs.createWriteStream(path);
}
}).listen(STREAM_PORT);
console.log('Listening for incomming MPEG-TS Stream on https://0.0.0.0:'+STREAM_PORT+'/<secret>');
console.log('Awaiting WebSocket connections on wss://0.0.0.0:'+WEBSOCKET_PORT+'/');
@lthrhx
Copy link

lthrhx commented Jul 25, 2019

I tried this script, but keeping getting "Firefox can't establish a connection to the server at wss://192.168.1.100:8082" when connecting from a client.

I do not get any errors when running it on the server.

Using the non secure ws relay script works fine. Are there other files or settings that need to be changed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment