console.log('clear arr: ' + clearnUP.join('-'));
我的问题是,在关闭客户端时,为什么clearUp
为空?
/* Beginners nodejs, fx, From China. */
var net = require('net');
var server = net.createServer();
var clients = [];
server.on('connection', function(client) {
client.write('hi\n\r');
clients.push(client);
client.on('data', function(data) {
printMsg(data, client);
});
client.on('end', function() {
clients.splice(clients.indexOf(client), 1);
});
client.on('error', function() {
console.log(err);
})
function printMsg(msg, client) {
var clearnUP = [];
for (var i = 0; i < clients.length; i++) {
client != clients[i] && (clients[i].writable ? clients[i].write(client.remotePort + ': ' + msg.toString() + '\n\r') : clearnUP.push(clients[i]) && clients[i].destroy() && clearnUP); // diswritable client push to clearUp<Array>
}
console.log(client.remotePort + ': ' + msg.toString());
console.log('clear arr: ' + clearnUP.join('-')); //when closed client, why clearUp is empty!!
}
//console.log(client);
}).listen(1220);