admin管理员组文章数量:1430407
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 :)
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 :)
Share Improve this question asked Dec 23, 2011 at 11:01 Antonio LagunaAntonio Laguna 9,3107 gold badges38 silver badges72 bronze badges1 Answer
Reset to default 7You should put the socket ids in the array like you did the first time, and on disconnect remove the socket.id of the disconnected client. You don't need to loop thorough an array to delete an element, you can achieve that using array.indexOf(element) to determine the position of the element and array.splice(position, 1) to delete that element:
function deleteFromArray(my_array, element) {
position = my_array.indexOf(element);
my_array.splice(position, 1);
}
io.sockets.on('connection', function (socket) {
clients.push(socket.id);
socket.broadcast.emit('conection', { id: clients});
socket.on('disconnect', function() {
deleteFromArray(clients, socket.id);
});
});
Resources:
Deleting array elements in JavaScript - delete vs splice
本文标签: javascriptHow to mantain current active sockets in socketionodejsStack Overflow
版权声明:本文标题:javascript - How to mantain current active sockets in socket.io + node.js? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745449017a2658797.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论