node.js 中的 HTTP 保持活动状态
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28229044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTTP keep-alive in node.js
提问by quentinadam
I am trying to set-up a HTTP client to keep the underlying connection open (keep-alive) in node.js, but it seems that the behaviour does not correspond to the docs (http://nodejs.org/api/http.html#http_class_http_agent).
我正在尝试设置一个 HTTP 客户端以在 node.js 中保持底层连接打开(保持活动状态),但该行为似乎与文档(http://nodejs.org/api/http .html#http_class_http_agent)。
I am creating a new HTTP agent, setting the maxSockets property to 1 and requesting an url (for instance http://www.twilio.com/) every second.
我正在创建一个新的 HTTP 代理,将 maxSockets 属性设置为 1 并每秒请求一个 url(例如http://www.twilio.com/)。
It seems that on every request the socket is closed and a new socket is created. I have tested this with node.js 0.10.25 and 0.10.36 under Ubuntu 14.04.
似乎每个请求都会关闭套接字并创建一个新的套接字。我已经在 Ubuntu 14.04 下使用 node.js 0.10.25 和 0.10.36 对此进行了测试。
Has anyone been able to get keep alive to work?
有没有人能够继续工作?
Here is the code:
这是代码:
var http = require("http");
var agent = new http.Agent();
agent.maxSockets = 1;
var sockets = [];
function request(hostname, path, callback) {
var options = {
hostname: hostname,
path: path,
agent: agent,
headers: {"Connection": "keep-alive"}
};
var req = http.get(options, function(res) {
res.setEncoding('utf8');
var body = "";
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', function () {
callback(null, res, body);
});
});
req.on('error', function(e) {
return callback(error);
});
req.on("socket", function (socket) {
if (sockets.indexOf(socket) === -1) {
console.log("new socket created");
sockets.push(socket);
socket.on("close", function() {
console.log("socket has been closed");
});
}
});
}
function run() {
request('www.twilio.com', '/', function (error, res, body) {
setTimeout(run, 1000);
});
}
run();
回答by simo
If I'm not mistaken the connection pool was implemented in 0.12.
如果我没记错的话,连接池是在 0.12 中实现的。
So if you want to have a connection pool prior 0.12 you can simply use the requestmodule:
所以如果你想在 0.12 之前有一个连接池,你可以简单地使用request模块:
var request = require('request')
request.get('www.twilio.com', {forever: true}, function (err, res, body) {});
If you are using node 0.12+ and you want to use the HTTP core module directly, then you can use this to initialize your agent:
如果您使用的是 node 0.12+ 并且您想直接使用 HTTP 核心模块,那么您可以使用它来初始化您的代理:
var agent = new http.Agent({
keepAlive: true,
maxSockets: 1,
keepAliveMsecs: 3000
})
Notice the keepAlive: trueproperty, that is requiredto keep the socket open.
请注意保持套接字打开所需的keepAlive: true属性。
You can pass an agent to the requestmodule as well, again that works only on 0.12+ otherwise it defaults to internal pool implementation.
您也可以将代理传递给请求模块,同样仅适用于 0.12+,否则默认为内部池实现。
回答by Vinay Prabhakaran
The below worked for me in meteor which uses the npm module for keepaliveagent
下面的流星对我有用,它使用 npm 模块进行 keepaliveagent
var agent = new KeepAliveAgent({ maxSockets: 1 });
var options = {
agent:agent,
headers: {"Connection":"Keep-Alive"}
}
try {
var client = Soap.createClient(url);
var result = client.myfirstfunction(args,options);
//process result
result = client.mysecondfunction(args,options);
}
Both the method calls returns data in one socket connection. You pass the options in each method call
这两种方法调用都在一个套接字连接中返回数据。您在每个方法调用中传递选项
回答by advncd
I guess it should work on node 0.12+. You may also want to use a different agent for this purpose. For example keep-alive-agentcan do what you want:
我想它应该适用于节点 0.12+。为此,您可能还想使用不同的代理。例如keep-alive-agent可以做你想做的事情:
var KeepAliveAgent = require('keep-alive-agent'),
agent = new KeepAliveAgent();

![node.js 在 fs.writeFile([option]) 中,“选项参数”通常如何工作?](/res/img/loading.gif)