我希望能够将远程 Icecast 流代理到客户端。这几天折腾了好久,还是没有结果。
用例:
能够从 <audio>
中提取分析器数据标签src
不会遇到 CORS 问题。
到目前为止我的解决方案
为了解决 CORS 问题,我无法直接从 <audio>
创建杠杆声音数据。的源代码,我尝试编写一个小型代理,它将请求通过管道传输到特定的流,并在任何其他情况下返回静态数据。
这是我的代码:
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 的数据,并且尽管代理已接收到后续数据 block ,但未收到后续数据 block :
欢迎任何建议!
最佳答案
我找到了解决方案,使用流管道。
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) => {
req.pipe(request.get(target)).pipe(res);
});
app.listen(PORT);
关于node.js - 如何在 Node 中代理媒体流?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46899772/