我是网络服务器和节点JS的新手,我跟随this tutorial创建 - 并了解 - 我的英特尔Edison开发板上的流媒体服务器。我已经在本网站上阅读了有关此问题的其他帖子,但我无法找到我的案例的答案
启动.js服务器后,我可以正确连接到它,在我的客户端浏览器中键入[我正在使用Chrome]类似于:http://192.168.1.31:8888/和视频,与教程一样,显示。如果我想我甚至可以激活它,它工作正常
但是看一下提供的代码:
http.createServer(function (req, res) {
var reqResource = url.parse(req.url).pathname;
//console.log("Resource: " + reqResource);
if(reqResource == "/"){
//console.log(req.headers)
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(indexPage);
res.end();
} else if (reqResource == "/favicon.ico"){
res.writeHead(404);
res.end();
} else {
var total;
if(reqResource == "/movie.mp4"){
total = movie_mp4.length;
} else if(reqResource == "/movie.ogg"){
total = movie_ogg.length;
} else if(reqResource == "/movie.webm"){
total = movie_webm.length;
}
var range = req.headers.range;
var positions = range.replace(/bytes=/, "").split("-");
var start = parseInt(positions[0], 10);
// if last byte position is not present then it is the last byte of the video file.
var end = positions[1] ? parseInt(positions[1], 10) : total - 1;
var chunksize = (end-start)+1;
if(reqResource == "/movie.mp4"){
res.writeHead(206, { "Content-Range": "bytes " + start + "-" + end + "/" + total,
"Accept-Ranges": "bytes",
"Content-Length": chunksize,
"Content-Type":"video/mp4"});
res.end(movie_mp4.slice(start, end+1), "binary");
} else if(reqResource == "/movie.ogg"){
res.writeHead(206, { "Content-Range": "bytes " + start + "-" + end + "/" + total,
"Accept-Ranges": "bytes",
"Content-Length": chunksize,
"Content-Type":"video/ogg"});
res.end(movie_ogg.slice(start, end+1), "binary");
} else if(reqResource == "/movie.webm"){
res.writeHead(206, { "Content-Range": "bytes " + start + "-" + end + "/" + total,
"Accept-Ranges": "bytes",
"Content-Length": chunksize,
"Content-Type":"video/webm"});
res.end(movie_webm.slice(start, end+1), "binary");
}
}
}).listen(8888);
我想我也应该能够输入我的客户端: http://192.168.1.31:8888/movie.mp4并正确解决此地址,以便在我的Chrome客户端上接收视频片段。问题是,当我尝试这样做时,我收到以下错误:
var positions = range.replace(/bytes=/, "").split("-");
^
TypeError: Cannot call method 'replace' of undefined
at Server.<anonymous> (/home/root/streaming-video-html5/streaming-video-html5/server.js:66:35)
at Server.emit (events.js:98:17)
at HTTPParser.parser.onIncoming (http.js:2109:12)
at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:122:23)
at Socket.socket.ondata (http.js:1967:22)
我从here了解到这个错误来自于range.replace.split
undefined
但是为什么?
如何解决这个烦人的问题?
提前感谢那些试图提供帮助的人
答案 0 :(得分:0)
客户端可能没有在请求标头中发送range
- 这不是强制性的,所以试试这个
var range = req.headers.range;
var start = 0, end = total - 1;
if(range) {
var positions = range.replace(/bytes=/, "").split("-");
start = parseInt(positions[0], 10);
// if last byte position is not present then it is the last byte of the video file.
end = positions[1] ? parseInt(positions[1], 10) : total - 1;
}
我认为应该解决它