如何在Node中代理媒体stream?
我想能够代理远程icecaststream到客户端。 在过去的几天里,我一直在摆弄很多东西,但没有成功。
用例 :
能够从<audio>
标签src
提取分析器数据,而不会遇到CORS问题。
我的解决scheme迄今
为了解决CORS问题,阻止我直接从<audio>
源文件中创build杠杆声音数据,我试着编写一个小代理,它将请求传递给特定的stream,并在任何其他情况下返回静态。 这是我的代码:
require('dotenv').config(); const http = require('http'); const express = require('express'); const app = express(); const PORT = process.env.PORT || 4000; let target = 'http://direct.fipradio.fr/live/fip-midfi.mp3'; // figure out 'real' target if the server returns a 302 (redirect) http.get(target, resp => { if(resp.statusCode == 302) { target = resp.headers.location; } }); app.use(express.static('dist')); app.get('/api', (req, res) => { http.get(target, audioFile => { res.set(audioFile.headers); audioFile.addListener('data', (chunk) => { res.write(chunk); }); audioFile.addListener('end', () => { res.end(); }); }).on('error', err => { console.error(err); }); }); app.listen(PORT);
问题
客户端接收到来自代理的响应,但是这被停止到60kb的数据,而后来的数据块没有被接收,尽pipe代理已经收到:
任何build议是值得欢迎的!