连接号不正确

aci*_*cid 3 node.js socket.io

我写了一个非常简单的脚本来检索连接用户列表.

var app, io, server;

app = require("express")();
server = require("http").createServer(app);
io = require("socket.io").listen(server);

server.listen(1339);

app.get("/", function(req, res) {
  res.sendfile(__dirname + "/index.html");
});

console.log('INIT', io.sockets.manager.server.connections);

io.sockets.on("connection", function(socket) {
  console.log('CONNECT: ', io.sockets.manager.server.connections);
  socket.on("disconnect", function(data) {
    console.log('DISCONNECT: ', io.sockets.manager.server.connections);
  });
});
Run Code Online (Sandbox Code Playgroud)

和HTML:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>title</title>
</head>
<body>
    <script src="/socket.io/socket.io.js"></script>
    <script>
        var socket = io.connect('http://localhost:1339');
    </script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

当我运行应用程序时,我进入0init,但是一旦我在浏览器中打开页面,它就会给我4连接而不仅仅是1.我也收到connections property is deprecated. Use getConnections() method警告.我在v0.10.15中使用节点,在v0.9.16中使用socket.io.

Wir*_*rie 5

您需要从其他位置获取客户端数量:

 io.sockets.clients().length
Run Code Online (Sandbox Code Playgroud)

但是,当您阅读此活动GitHub问题时,您可能希望避免该操作,因为它可能导致额外的内存泄漏(超出套接字已经发生的事情).

这是一个完整的工作示例:

var app = require('express')();
var server = require("http").createServer(app);
var io = require('socket.io').listen(server);

server.listen(1339);

app.get('/', function(req, res) {
    return res.sendfile(__dirname + "/index.html");
});

/*
    to obtain number of connected clients
        io.sockets.clients().length            
 */

console.log("INIT", io.sockets.clients().length );

io.sockets.on("connection", function(socket) {
    console.log("CONNECT:" + io.sockets.clients().length);
    socket.on("disconnect", function(data){
        console.log("DISCONNECT: ", io.sockets.clients().length);
    });
});
Run Code Online (Sandbox Code Playgroud)

断开事件发生在客户端被删除之前,因此计数将比客户端总数高一个(取决于您如何看待它).

此外,on由于某种原因,您让事件处理程序返回值,因此我删除了它.

只使用一个简单的计数器,您就可以获得更好的运气,而不是使用该功能:

var clientsConnected = 0;

console.log("INIT", io.sockets.clients().length );

io.sockets.on("connection", function(socket) {
    console.log("CONNECT:" + ++clientsConnected);
    socket.on("disconnect", function(data){
        console.log("DISCONNECT: ", --clientsConnected);
    });
});
Run Code Online (Sandbox Code Playgroud)

有些人显然报告断开连接的数量可能超过连接数,因此您可能希望防止计数器降至零以下.

连接两个客户端(然后断开一个):

info: socket.io started
INIT 0
debug: served static content /socket.io.js
debug: client authorized
info: handshake authorized eJcvVsr60fmRzhVpQ6rO
debug: setting request GET /socket.io/1/websocket/eJcvVsr60fmRzhVpQ6rO
debug: set heartbeat interval for client eJcvVsr60fmRzhVpQ6rO
debug: client authorized for 
debug: websocket writing 1::
CONNECT:1
debug: served static content /socket.io.js
debug: client authorized
info: handshake authorized 28lUudTS3KtCphd0Q6rP
debug: setting request GET /socket.io/1/websocket/28lUudTS3KtCphd0Q6rP
debug: set heartbeat interval for client 28lUudTS3KtCphd0Q6rP
debug: client authorized for 
debug: websocket writing 1::
CONNECT:2
info: transport end (socket end)
debug: set close timeout for client 28lUudTS3KtCphd0Q6rP
debug: cleared close timeout for client 28lUudTS3KtCphd0Q6rP
debug: cleared heartbeat interval for client 28lUudTS3KtCphd0Q6rP
DISCONNECT:  2
debug: discarding transport
info: transport end (socket end)
debug: set close timeout for client eJcvVsr60fmRzhVpQ6rO
debug: cleared close timeout for client eJcvVsr60fmRzhVpQ6rO
debug: cleared heartbeat interval for client eJcvVsr60fmRzhVpQ6rO
DISCONNECT:  1
debug: discarding transport
Run Code Online (Sandbox Code Playgroud)