I'm starting to learn socket.io and node.js I'm trying to do some pretty basic stuff (or so I think) but I'm unable to do.
Here is my node app:
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
app.listen(8080);
var clients = [];
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
io.sockets.on('connection', function (socket) {
clients[socket.id] = true;
socket.broadcast.emit('conection', { id: clients});
});
I want to store connected clients and then, onConnection send them all clients connected. The point is, I don't know how to properly use the arrays on JavaScript because using clients.push(socket.id)
functions well BUT then, I won't be able to pop
it a socket.id
once a client disconnect without looping through the array, right?
Even if there is a method to obtain the current opened sockets, I want to do it in this way because I won't use the application with current socket sessions but with other thing.
I know it's a really noob question so, please, bear with me :)