0

我对网络服务器和节点 JS 完全陌生,我正在按照本教程在我的英特尔爱迪生板上创建并理解流媒体服务器。我已经在这个网站上阅读了关于这个问题的其他帖子,但我找不到我的案例的答案

启动 .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)

我从这里了解到这个错误来自拥有range.replace.split undefined但为什么呢?

关于如何解决这个烦人的问题的任何想法?

提前感谢那些愿意提供帮助的人

4

1 回答 1

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;
    }

我认为应该解决它

于 2015-07-10T04:30:14.293 回答