node.js Socket.io 连接用户数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10275667/
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
Socket.io Connected User Count
提问by imperium2335
I finally got socket.io to work properly, but I have encountered a strange problem.
我终于让socket.io正常工作了,但是我遇到了一个奇怪的问题。
I am not sure if this is the best way, but I am using:
我不确定这是否是最好的方法,但我正在使用:
io.sockets.clients().length
This returns the number of clients connected to my server. The problem is after a few connects and disconnects of users, the number starts to stay higher than it should be.
这将返回连接到我的服务器的客户端数量。问题是在用户几次连接和断开连接后,数字开始保持高于应有的水平。
For instance, if I connect and ask my friends to, the number goes up which is correct. But when we start to disconnect and reconnect the number does not decrease.
例如,如果我连接并询问我的朋友,数字会上升,这是正确的。但是当我们开始断开连接并重新连接时,数量并没有减少。
I am running the node.js and sockets.io server on a vmware ubuntu server.
我在 vmware ubuntu 服务器上运行 node.js 和 sockets.io 服务器。
Does anyone know why this is or has a better method for finding out how many people are connected to the server?
有谁知道这是为什么或有更好的方法来找出有多少人连接到服务器?
采纳答案by drinchev
There is a github issuefor this. The problem is that whenever someone disconnects socket.io doesn't delete ( splice ) from the array, but simply sets the value to "null", so in fact you have a lot of null values in your array, which make your clients().length bigger than the connections you have in reality.
有一个github 问题。问题是,每当有人断开 socket.io 时,不会从数组中删除( splice ),而是简单地将值设置为“null”,因此实际上您的数组中有很多空值,这使您的客户端( ).length 比你在现实中拥有的连接大。
You have to manage a different way for counting your clients, e.g. something like
您必须管理一种不同的方式来计算您的客户,例如
socket.on('connect', function() { connectCounter++; });
socket.on('disconnect', function() { connectCounter--; });
It's a mind buzz, why the people behind socket.io have left the things like that, but it is better explain in the github issue, which I posted as a link!
这是一个思想嗡嗡声,为什么socket.io背后的人留下了这样的东西,但最好在github问题中解释,我作为链接发布了!
回答by Mody
Just in case someone gets to this page while using socket.io version 1.0
以防万一有人在使用 socket.io 1.0 版时访问此页面
You can get the connected clients count from
您可以从
socketIO.engine.clientsCount
Need an answer and the above did not work for new version of socket.io
需要一个答案,以上对新版本的 socket.io 不起作用
回答by Lordran
I have found the way to figure it out in version 1.3.7. There are three methods as follows:
我在 1.3.7 版中找到了解决方法。有以下三种方法:
io.engine.clientsCountio.sockets.sockets.lengthObject.keys(io.sockets.connected).length
io.engine.clientsCountio.sockets.sockets.lengthObject.keys(io.sockets.connected).length
Hope these can help someone with the same issue.:)
希望这些可以帮助有同样问题的人。:)
回答by rafa226
with socket.io 2.2.0 it's easier :
使用 socket.io 2.2.0 更容易:
io.on('connection', function (socket) {
console.log( socket.client.conn.server.clientsCount + " users connected" );
});
cheers
干杯
回答by line-o
Why use an (implicit global) variable when you could always filter the array, that is returned by calling the clients() method.
当您始终可以过滤通过调用 clients() 方法返回的数组时,为什么要使用(隐式全局)变量。
function filterNullValues (i) {
return (i!=null);
}
io.sockets.clients().filter(filterNullValues).length;
回答by devtanc
I am currently using Socket.io v1.3.6 and have found that this works. It gives an accurate number when users connect and when they disconnect:
我目前正在使用 Socket.io v1.3.6 并发现它有效。当用户连接和断开连接时,它会给出准确的数字:
io.sockets.sockets.length
Like so:
像这样:
var io = require('socket.io').listen(server);
io.on('connection', function(socket) {
console.log(io.sockets.sockets.length);
socket.on('disconnect', function() {
console.log(io.sockets.sockets.length);
});
});
回答by dknaus
I'm using socket.io 0.9.10 and the following code to determine the number of sockets:
我正在使用 socket.io 0.9.10 和以下代码来确定套接字的数量:
var socketIO = require('socket.io').listen( .....
var numberOfSockets = Object.keys(socketIO.connected).length;
Not sure how accurate this number reacts to the various edge-cases, but 'til now it seems accurate: every browser connecting increases the number, every browser closed decreases it.
不确定这个数字对各种边缘情况的反应有多准确,但到目前为止它似乎是准确的:每个浏览器连接都会增加数字,每个浏览器关闭都会减少它。
回答by Gilbert Flamino
Also take a look into:
还可以看看:
io.sockets.manager.connected
io.sockets.manager.connected
It's a clean list of key value pairs (socket id and connection state?)
这是一个干净的键值对列表(套接字 ID 和连接状态?)
回答by Pavani dasari
Connected Users count in number with socket.io version - 1.3.7
连接用户数与 socket.io 版本 - 1.3.7
io.on('connection', (socket) => {
console.log(io.sockets.server.httpServer._connections); //output in number
// or
console.log(io.sockets.server.engine.clientsCount); //output in number
});
回答by Tristan CHARBONNIER
Tested using Socket.IO v2.3.0 using namespace, I found 4 locations having the clientCounts property (it's probably the same Serverobject each time):
使用命名空间使用 Socket.IO v2.3.0 进行测试,我发现 4 个位置具有 clientCounts 属性(Server每次可能都是同一个对象):
const socketio = require('socket.io');
const io = socketio(http_server);
const io_namespace = io.of('/foobar');
io_namespace.on('connection', function(socket)
{
console.log(socket.conn.server.clientsCount);
console.log(socket.server.engine.clientsCount);
console.log(io.engine.clientsCount);
console.log(io_namespace.server.engine.clientsCount);
});

