当我关闭任何客户端时,为什么clearUp:Array为空?

时间:2015-07-30 01:36:56

标签: javascript node.js

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);

0 个答案:

没有答案