k-yle / rtsp-relay

📽 View an RTSP stream in your web browser using an express.js server
https://npm.im/rtsp-relay
MIT License
325 stars 59 forks source link

Proper way to terminate app.ws connection at given seconds ? #107

Closed axacheng closed 2 years ago

axacheng commented 2 years ago

I'd like to stream ffmpeg output in just 60 seconds for every web client connections. I've tried ws.close() or ws.terminate() that closed the connection but rtsp-relay built-in function did reconnect, so web client still can see proxy({}) output. Could you please provide a code example to terminate connection?

Thanks

k-yle commented 2 years ago

Hi @axacheng, if you terminate the stream after 60 seconds, should the client be allowed to reconnect immediately? If not, you will need to write you own logic to allow or disallow certain requests. That would be out of the scope of this module.

Here's an example that allows or disallows access to the stream:

const express = require('express');
const app = express();

const { proxy } = require('rtsp-relay')(app);

app.ws('/api/stream', (ws, req) => {
  if (!isAuthenticated(req)) {
    // NOT authenticated. do not allow access to stream
    return;
  }
  return proxy({ url: 'rtsp://1.2.3.4/cam1' })(ws);
});
axacheng commented 2 years ago

ah~ awesome! I was also thinking to implement Auth Bearer header to do so, but would be nice to check with you first.

Thank you so much