如何找出在Node.js中有多less连接的客户机到HTTP服务器?
假设我正在运行一个应用程序,该应用程序使用Express和Socket.io,都在Node http.Server
的同一个实例上进行http.Server
。
每秒我都要login到控制台连接的客户端(HTTP或WS)的总数为监视目的。
setInterval(logConnectedClientInfo, 1000);
我怎样才能实现方法logConnectedClientInfo()
?
我知道我可以通过exec
lsof
来做到这一点,但我有兴趣,如果有一种方法可以在Node中做到这一点?
这会工作吗?
server.on('connection', function (socket) { activeConnections++; socket.on('end', function() { activeConnections--; }); });
我find了答案。 net
模块在一个名为server.getConnections()
的服务器上有一个方法。 节点(0.10.30)中的net.Server
从net.Server
inheritance。 请参阅节点核心中的lib/_http_server.js
:
util.inherits(Server, net.Server);
所以我的方法看起来像:
function logConnectedClientInfo() { server.getConnections(function(err, count){ if(err) throw err; console.log(count); }); }