Node.js req.headers.range无法找到来自android设备的呼叫?
我尝试在nodeJS中制作音频文件流。但如果我从浏览器中调用,它仍然可以正常工作,但如果从任何设备调用,它将无法工作,因为找不到范围Node.js req.headers.range无法找到来自android设备的呼叫?,node.js,http-headers,streaming,audio-streaming,Node.js,Http Headers,Streaming,Audio Streaming,我尝试在nodeJS中制作音频文件流。但如果我从浏览器中调用,它仍然可以正常工作,但如果从任何设备调用,它将无法工作,因为找不到范围 //If call from Browser req.headers output like { host: 'localhost:7060', connection: 'keep-alive', 'user-agent': 'stagefright/1.2 (Linux;Android 4.4.4)', 'accept-encoding': 'gzi
//If call from Browser req.headers output like
{ host: 'localhost:7060',
connection: 'keep-alive',
'user-agent': 'stagefright/1.2 (Linux;Android 4.4.4)',
'accept-encoding': 'gzip,deflate',
range: 'bytes=1-696319' }
//If call from devices
{ host: 'localhost',
'user-agent': 'stagefright/1.1 (Linux;Android 2.3.6)' }
只有输出发生我在这里找不到任何范围我能做什么
app.js文件
var fs = require('fs'),
queryString = require('querystring');
var query, URI;
exports.streamMP3 = function(req, res){
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header("Access-Control-Allow-Credentials", "true");
res.header("crossDomain","true");
res.header("Range","bytes=1-999");
//console.log(req);
try{
query = URI = null;
//console.log("METHOD:", req.method);
if(req.method=='POST') {
query = req.body;
}else{
URI = decodeURIComponent(req.url);
if (req.url.indexOf('?') >= 0) {
query = queryString.parse(req.url.replace(/^.*\?/, ''));
}
}
console.log("QUERY:", query);
var audio_file_path = null;
var audio_file_id = null;
if(query && query.id){
audio_file_id = query.id;
audio_file_path = './mp3/'+audio_file_id+'.mp3';
}else{
audio_file_path = './mp3/tmp.mp3';
}
fs.readFile(audio_file_path, "binary", function(err, file) {
var header = {};
// add content type to header
//req.headers.range = 'bytes=1-696319';
console.log(req.headers);
//TODO: any more clean solution ?
if(typeof req.headers.range != 'undefined'){
// browser wants chunged transmission
var range = req.headers.range;
var parts = range.replace(/bytes=/, "").split("-");
var partialstart = parts[0];
var partialend = parts[1];
var total = file.length;
var start = parseInt(partialstart,10);
var end = partialend ? parseInt(partialend,10) : total-1;
var diff = end - start;
var value = parseInt((partialend*20)/100,10);
if(diff > value){
end = start + value;
}
var chunksize = (end-start)-10000;
console.log(start , end);
//var end = parseInt((partialend*20)/100, 10);
header["Content-Range"] = "bytes " + start + "-" + end + "/" + (total);
header["Accept-Ranges"] = "bytes";
header["Content-Length"]= chunksize;
header['Transfer-Encoding'] = 'chunked';
header["Connection"] = "close";
//console.log(start, end);
res.writeHead(206, header);
// yeah I dont know why i have to append the '0'
// but chrome wont work unless i do
//res.end(file.slice(start, chunksize), "binary");
res.write(file.slice(start, end)+'0', "binary");
}else{
console.log("Unchunked");
// reply to normal un-chunked request
res.writeHead(200, header );
res.write(file, "binary");
}
res.end();
});
}catch(e){
console.log("TRY ERROR:", e);
res.end();
}
}
并非所有客户端都会请求特定的范围。事实上,大多数人没有。您的代码应该能够通过简单地返回从字节0到末尾的整个资源来处理这些请求。并非所有客户端都会请求特定的范围。事实上,大多数人没有。您的代码应该能够通过简单地返回从字节0到末尾的整个资源来处理这些请求。并非所有客户端都会请求特定的范围。事实上,大多数人没有。您的代码应该能够通过简单地返回从字节0到末尾的整个资源来处理这些请求。并非所有客户端都会请求特定的范围。事实上,大多数人没有。您的代码应该能够通过简单地返回从字节0到末尾的整个资源来处理这些请求