连接号不正确

时间:2013-08-26 14:05:49

标签: 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);
  });
});

和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>

当我运行应用时,我会在初始化时获得0,但只要我在浏览器中打开该页面,它就会提供4个连接,而不仅仅是1。我也收到connections property is deprecated. Use getConnections() method警告。我在v0.10.15中使用node,在v0.9.16中使用socket.io。

1 个答案:

答案 0 :(得分:5)

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

 io.sockets.clients().length

但是,当您阅读this active GitHub issue时,您可能希望避免该操作,因为它可能导致额外的内存泄漏(超出套接字已经发生的情况)。

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

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

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

此外,由于某种原因,您让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);
    });
});

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

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

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