node.js - Incorrect connections number -
i wrote simple script retrieve list of connected users.
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); }); });
and 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>
when run app 0
on init, open page in browser gives me 4
connections instead of 1
. connections property deprecated. use getconnections() method
warning. using node in v0.10.15 , socket.io in v0.9.16.
you'll need obtain number of clients different location:
io.sockets.clients().length
however, when read this active github issue, might want avoid operation can cause additional memory leak (beyond what's happening sockets).
here's complete working example:
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"); }); /* 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); }); });
the disconnect event happens before client has been removed, count 1 higher total number of clients (depending on how @ it).
also, had on
event handlers returning values reason, removed that.
instead of using function, might have better luck simple counter:
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); }); });
some have apparently reported number of disconnects can exceed number of connections, may want prevent counter dropping below zero.
connected 2 clients (and disconnected one):
info: socket.io started init 0 debug: served static content /socket.io.js debug: client authorized info: handshake authorized ejcvvsr60fmrzhvpq6ro debug: setting request /socket.io/1/websocket/ejcvvsr60fmrzhvpq6ro debug: set heartbeat interval client ejcvvsr60fmrzhvpq6ro debug: client authorized debug: websocket writing 1:: connect:1 debug: served static content /socket.io.js debug: client authorized info: handshake authorized 28luudts3ktcphd0q6rp debug: setting request /socket.io/1/websocket/28luudts3ktcphd0q6rp debug: set heartbeat interval client 28luudts3ktcphd0q6rp debug: client authorized debug: websocket writing 1:: connect:2 info: transport end (socket end) debug: set close timeout client 28luudts3ktcphd0q6rp debug: cleared close timeout client 28luudts3ktcphd0q6rp debug: cleared heartbeat interval client 28luudts3ktcphd0q6rp disconnect: 2 debug: discarding transport info: transport end (socket end) debug: set close timeout client ejcvvsr60fmrzhvpq6ro debug: cleared close timeout client ejcvvsr60fmrzhvpq6ro debug: cleared heartbeat interval client ejcvvsr60fmrzhvpq6ro disconnect: 1 debug: discarding transport
Comments
Post a Comment