2016-07-27 4 views
1

ホスト(api.development.push.apple.com)への永続的なhttp接続を作成し、多数のパスのPOSTリクエストを送信したいと考えています。 '/ 3/device/1'、 '/ 3/device/2'など)を使用することができます。以下のコードは、http.request()ごとに1つのホストまたは複数の接続との接続を作成しますか?何をしたいnode.js http:ホストへの永続接続を作成し、複数のパスにリクエストを送信する

var http = require('http'); 

http.request({ 
    host: 'api.development.push.apple.com', 
    port: 443, 
    path: '/3/device/1', 
    method: 'POST', 
}).end(); 

http.request({ 
    host: 'api.development.push.apple.com', 
    port: 443, 
    path: '/3/device/2', 
    method: 'POST' 
}).end(); 

答えて

2

はあなたの要求のすべてに対して同じエージェントを使用することです。

optionsオブジェクトにエージェントを指定しない場合、httpモジュールはglobalAgentを使用します。この場合、keepAliveはデフォルトでfalseに設定されます。

だから、あなたのエージェントを作成し、すべての要求のためにそれを使用します。

var http = require('http'); 
var agent = new http.Agent({ keepAlive: true }); // false by default 

http.request({ 
    host: 'api.development.push.apple.com', 
    port: 443, 
    path: '/3/device/1', 
    method: 'POST', 
    agent: agent, // use this agent for more requests as needed 
}).end(); 
関連する問題