问题
I need to get count of websockets online.
I trying to calculate this that:
const socketIo = require('socket.io');
const app = require('express')();
const http = require('http').Server(app);
var online = 0;
var port = 6001;
var io = socketIo(port);
//I have only one NameSpace and root NS is not used
var ns1 = io.of('ns1');
ns1
.on('connection', function (socket) {
online += 1;
socket.on('disconnect', function (reason) {
online -= 1;
});
});
...
app.get('/', function (req, res) {
res.send({
clientsCount: io.engine.clientsCount,
online: online
});
});
http.listen(8000, function () {
console.log('Web app is listening');
});
I have a problem: why "online" not equal "clientsCount"?
What is right way to calculate count of websockets online using events callbacks?
回答1:
make sure your client connect to the same namespace, something like the following code:
io('http://localhost:6001/ns1'); // client connection
来源:https://stackoverflow.com/questions/48375213/socket-io-how-to-calculate-count-of-websockets-online-using-callbacks-of-conne