2011-07-12 3 views
3

コマンドラインで実行すると、プログラムはclient.destroy()の後で停止します。Node.js http.request接続をうまく破壊するには?

var client = http.get(options, 
     function(res) { 
      console.log(res.statusCode); 
      res.on('end', function() { 
       console.log("done"); 
       client.destroy(); 
      }); 
      } 
); 

それはhttp.socketので、例外がスローされます:私はres.on( '終了')の内側()client.destroyを置くとき

var client = http.get(options, 
     function(res) { 
      console.log(res.statusCode); 
      client.destroy(); 
      } 
); 

しかし、それはそうではありません無効である。そう、私はそれを破壊することはできません。

この場合、プログラムの実行がハングして終了しません。それをやめるために私は何ができますか? (process.exit()を除く)。

答えて

4

それはあなただけで偽

var request = http.get(options, 
     function(res) { 
      console.log(res.statusCode); 
      } 
); 
request.shouldKeepAlive = false 

またはI「の要求を行うとき、それはthe request 'end' event handler

+1

detachedあるので、あなたがソケットにアクセスすることはできませんConnection: closeヘッダ

を送信するshouldKeepAliveを設定することができ、単一のリクエストの場合.shouldKeepAlive "、それはソケットプーリングの何かに何か影響を及ぼしますか? – murvinlai

+0

Connectionヘッダーを使用するほうが良いでしょう、ここで分析します - https://github.com/joyent/node/blob/master/lib/htt p.js#L442これは、要求shouldKeepAliveフラグ –

+0

に影響します。それを試してみましょう。 – murvinlai

関連する問題