websocket-relay.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Use the websocket-relay to serve a raw MPEG-TS over WebSockets. You can use
  2. // ffmpeg to feed the relay. ffmpeg -> websocket-relay -> browser
  3. // Example:
  4. // node websocket-relay yoursecret 8081 8082
  5. // ffmpeg -i <some input> -f mpegts http://localhost:8081/yoursecret
  6. var fs = require('fs'),
  7. http = require('http'),
  8. WebSocket = require('ws');
  9. if (process.argv.length < 3) {
  10. console.log(
  11. 'Usage: \n' +
  12. 'node websocket-relay.js <secret> [<stream-port> <websocket-port>]'
  13. );
  14. process.exit();
  15. }
  16. var STREAM_SECRET = process.argv[2],
  17. STREAM_PORT = process.argv[3] || 8081,
  18. WEBSOCKET_PORT = process.argv[4] || 8082,
  19. RECORD_STREAM = false;
  20. // Websocket Server
  21. var socketServer = new WebSocket.Server({port: WEBSOCKET_PORT, perMessageDeflate: false});
  22. socketServer.connectionCount = 0;
  23. socketServer.on('connection', function(socket, upgradeReq) {
  24. socketServer.connectionCount++;
  25. console.log(
  26. 'New WebSocket Connection: ',
  27. (upgradeReq || socket.upgradeReq).socket.remoteAddress,
  28. (upgradeReq || socket.upgradeReq).headers['user-agent'],
  29. '('+socketServer.connectionCount+' total)'
  30. );
  31. socket.on('close', function(code, message){
  32. socketServer.connectionCount--;
  33. console.log(
  34. 'Disconnected WebSocket ('+socketServer.connectionCount+' total)'
  35. );
  36. });
  37. });
  38. socketServer.broadcast = function(data) {
  39. socketServer.clients.forEach(function each(client) {
  40. if (client.readyState === WebSocket.OPEN) {
  41. client.send(data);
  42. }
  43. });
  44. };
  45. // HTTP Server to accept incomming MPEG-TS Stream from ffmpeg
  46. var streamServer = http.createServer( function(request, response) {
  47. var params = request.url.substr(1).split('/');
  48. if (params[0] !== STREAM_SECRET) {
  49. console.log(
  50. 'Failed Stream Connection: '+ request.socket.remoteAddress + ':' +
  51. request.socket.remotePort + ' - wrong secret.'
  52. );
  53. response.end();
  54. }
  55. response.connection.setTimeout(0);
  56. console.log(
  57. 'Stream Connected: ' +
  58. request.socket.remoteAddress + ':' +
  59. request.socket.remotePort
  60. );
  61. request.on('data', function(data){
  62. socketServer.broadcast(data);
  63. if (request.socket.recording) {
  64. request.socket.recording.write(data);
  65. }
  66. });
  67. request.on('end',function(){
  68. console.log('close');
  69. if (request.socket.recording) {
  70. request.socket.recording.close();
  71. }
  72. });
  73. // Record the stream to a local file?
  74. if (RECORD_STREAM) {
  75. var path = 'recordings/' + Date.now() + '.ts';
  76. request.socket.recording = fs.createWriteStream(path);
  77. }
  78. })
  79. // Keep the socket open for streaming
  80. streamServer.headersTimeout = 0;
  81. streamServer.listen(STREAM_PORT);
  82. console.log('Listening for incomming MPEG-TS Stream on http://127.0.0.1:'+STREAM_PORT+'/<secret>');
  83. console.log('Awaiting WebSocket connections on ws://127.0.0.1:'+WEBSOCKET_PORT+'/');