node.js中的HTTP keep-alive [英] HTTP keep-alive in node.js

查看:1753
本文介绍了node.js中的HTTP keep-alive的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试建立一个HTTP客户端,以便在node.js中保持底层连接打开(保持活动状态),但似乎行为与文档不对应( http://nodejs.org/api/http.html#http_class_http_agent )。

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代理,将maxSockets属性设置为1并请求一个url(例如 http://www.twilio.com/ )每一秒。

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.

似乎每次请求都关闭套接字并创建一个新的套接字。
我在Ubuntu 14.04下使用node.js 0.10.25和0.10.36对此进行了测试。

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.

有没有人能够继续工作?

Has anyone been able to get keep alive to work?

以下是代码:

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();


推荐答案

如果我没弄错,连接池已实现在0.12。

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 request module:

var request = require('request')
request.get('www.twilio.com', {forever: true}, function (err, res, body) {});

如果您使用的是节点0.12+而您想直接使用HTTP核心模块,那么您可以用它来初始化你的代理:

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:

var agent = new http.Agent({
  keepAlive: true,
  maxSockets: 1,
  keepAliveMsecs: 3000
})

注意 keepAlive:true 属性,必需以保持套接字打开。

Notice the keepAlive: true property, that is required to keep the socket open.

你可以将代理传递给请求模块,再次仅适用于0.12+,否则默认为内部池实现。

You can pass an agent to the request module as well, again that works only on 0.12+ otherwise it defaults to internal pool implementation.

这篇关于node.js中的HTTP keep-alive的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆