2016-10-28 12 views
2

(不要なバックストーリー) 私はウェブカメラフィードをストリーミングするexpressjsフレームワークを持つnodejsサーバーを持っています。私が必要とするのは、複雑なCORSの問題のために、このサーバからmjpgストリームが来なければならないからです。Nodejsリクエストプロキシストリーム(mjpeg)接続が終了しません

//proxy from webcam server to avoid CORS complaining 
app.get('/stream1',function(req,res){ 
    var url="http://camera.nton.lviv.ua/mjpg/video.mjpg" 
    request(url).pipe(res); 
}); 

質問: 問題は簡単です。 request(url).pipe(res)は決して閉じません。なぜなら、ソースは文字通り終了しないmjpegですからです。私は、クライアント(ブラウザ;宛先)がもはや利用できなくなったときに、このパイプを強制的に閉じる方法を見つける必要があります - ウィンドウを閉じます。

enter image description here

+0

あなたは、クライアントがまだ使用可能であるかどうかを確認するためのWebSocketまたはいくつかの長いプーリング法を使用する必要があります。 – magreenberg

答えて

0

を監視するためにsocket.io。クライアント接続を閉じるためのイベントリスナーを追加し、発生したときにパイプを強制的に閉じます。

app.get('/stream1',function(req,res){ 
    var url="http://camera.nton.lviv.ua/mjpg/video.mjpg" 
    var pipe=request(url).pipe(res); 
    pipe.on('error', function(){ 
     console.log('error handling is needed because pipe will break once pipe.end() is called') 
    } 
    //client quit normally 
    req.on('end', function(){ 
     pipe.end(); 
    } 
    //client quit unexpectedly 
    req.on('close', function(){ 
     pipe.end(); 
    } 
}); 
0

使用すると、リモート接続、私は簡単な方法を見出した

// install it on your project 
 
npm install socket.io 
 

 
// require it on server side 
 
var socket = require('socket.io'); 
 

 
// listen for sockets from your server 
 
var mysocks = socket.listen(myexpressappvar); 
 

 
// keep collection of sockets for use if needed 
 
// its good practice 
 
var connectedSockets = []; 
 

 
// add event handelers on server side 
 
mysocks.sockets.on("connection", function(socket){ 
 
    
 
    // add socket to our collection 
 
    connectedSockets.push(socket); 
 
    
 
    // you will need to bind their stream id here. 
 
    exe..... 
 
    
 
    // listen for disconnected 
 
    socket.on("disconnect", function(){ 
 
     // remove socket from collection 
 
     connections.splice(connections.indexOf(socket), 1); 
 
     // destory stream here 
 
     exe... 
 
    }); 
 
}); 
 
    
 
// last thing, is add socket.io to the client side. 
 
<script src="/socket.io/socket.io.js"></script> 
 

 
// then call connect from client side js file 
 
var socket = io.connect();

+0

お返事ありがとうございますが、私はより簡単なアプローチを見つけたと思います! :)ありがとう〜 –

0

他の回答は私のためには機能しませんでした。 この行は、var pipe = request(url).pipe(res); は、要求オブジェクトの代わりにパイプを返します。だから私はラインを壊す必要があった。

中止するには要求オブジェクトが必要です。 .end()を呼び出すことはできませんでしたが、.abort()はそのトリックを行いました。私のために働く答えを見つけるのに数時間かかったので、分かち合うと思った。

app.get('/cam/frontdoor',function(req,res){ 
 

 
     var request_options = { 
 
      auth: { 
 
       user: '', 
 
       pass: ''}, 
 
      url: 'http:/xx.xx.xx.xx/mjpg/video.mjpg', 
 
     }; 
 

 
     var req_pipe = request(request_options); 
 
     req_pipe.pipe(res); 
 

 
     req_pipe.on('error', function(e){ 
 
      console.log(e) 
 
     }); 
 
     //client quit normally 
 
     req.on('end', function(){ 
 
      console.log('end'); 
 
      req_pipe.abort(); 
 

 
     }); 
 
     //client quit unexpectedly 
 
     req.on('close', function(){ 
 
      console.log('close'); 
 
      req_pipe.abort() 
 

 
     }) 
 

 

 
    })

関連する問題