var net = require('net');
var chatServer = net.createServer(), clientList = [] chatServer.on('connection', function(client) {
client.name = client.remoteAddress + ':' + client.remotePort; client.write('Hi' + client.name + '!\n');
clientList.push(client)
client.on('data', function(data) {
broadcast(data, client);
});
client.on('end', function() {
clientList.splice((clientList.indexOf(client, 1)))
})
client.on('error', function(e) {
console.log(e);
})
});
function broadcast(message, client) {
var cleanup = []
for(var i=0; i<clientList.length; i+=1) {
if(client !== clientList[i]) {
if(clientList[i].writable) {
clientList[i].write(client.name + ' say ' + message)
}else {
cleanup.push(clientList[i])
clientList[i].destroy()
console.log('malegeb')
}
}
for(i=0; i<cleanup.length; i+=1) {
console.log('malegeb')
clientList.splice(clientList.indexOf(cleanup[i]), 1)
}
}
}
chatServer.listen(9000);
在一个客户端发送信息,另一个客户端只能接收到第一个字符就停止接收了,但是另一个客户端也没有关闭,这是怎么回事?
1 回复