2015-07-10 34 views
0

我對web服務器和節點JS絕對陌生,並且我正在跟隨this tutorial創建並理解我的英特爾Edison開發板上的流媒體服務器。我已經閱讀了這個網站上關於這個問題的其他帖子,但我找不到我的案例的答案流媒體服務器:TypeError:無法調用未定義的方法'替換'

啓動.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.splitundefined但爲什麼呢?

任何想法如何解決這個惱人的問題?

在此先感謝那些誰將會盡力幫助

回答

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

我認爲應該修復它

+0

你的解釋是錯誤的原因,你的解決方案修復了地址問題,之後,我可以正確鍵入http://192.168.1.31:8888/movie.mp4觀看視頻,而無需服務器崩潰。但是現在像192.168.1.31:8888/videolist.html這樣在服務器上加載任何html頁面會導致無盡的過程,甚至無用,因爲最後不加載元素。所以: 1)關於原因和解決方案的任何想法? 2)這是我的錯誤印象還是當範圍沒有提供流媒體服務器作爲一個文件服務器?與一個60MB的視頻我看不到它 – Antonino

+0

我的想法是「手動」將塊分成一個循環直到最後一個字節,根據你的意思嗎?感謝你的寶貴時間 – Antonino

相關問題