我想建立与主机(api.development.push.apple.com)的持久http连接,并发送许多路径(例如,“ / 3 / device / 1”,“ / 3 / device / 2”,等等。)。下面的代码将为主机创建一个连接,还是为每个http.request()创建多个连接?
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();
Run Code Online (Sandbox Code Playgroud)
小智 5
您想要的是对所有请求使用相同的代理。
如果您未在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();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1918 次 |
| 最近记录: |