我開始學習socket.io和node.js我試圖做一些非常基本的東西(或者我認爲),但我無法做到。如何維護socket.io + node.js中的當前活動套接字?
這裏是我的節點的應用程序:
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});
});
我想存儲連接的客戶端,然後,在OnConnection把他們連接的所有客戶端。問題是,我不知道如何正確使用JavaScript上的數組,因爲使用clients.push(socket.id)
的功能很好,但是,一旦客戶端斷開連接而沒有循環訪問數組,我將無法使用pop
這是一個socket.id
,對吧?
即使有一種方法來獲取當前打開的套接字,我想這樣做,因爲我不會將該應用程序與當前套接字會話一起使用,而是與其他事物一起使用。
我知道這是一個真正的noob問題的話,請多多包涵:)
非常感謝你@alessioalex它的工作了,我已經學到新的東西:) – 2011-12-23 11:40:59
很樂意幫忙,由你也可以給予好評的接受的答案的方式。 – alessioalex 2011-12-23 11:46:41