我有一个node.js聊天服务器的以下代码。服务器运行,客户端可以进行telnet和连接。但是当一个客户端发送一条消息时,它会自动断开其他所有客户端的连接,我不明白自己做错了什么。

var net = require('net');
var chatServer = net.createServer();
var 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++) {
        if(client !== clientList[i]) {
            if(clientList[i].writeable) {
                clientList[i].write(client.name + " says " + message);
            } else {
                cleanup.push(clientList[i]);
                clientList[i].destroy();
            }
        }
    }

    for(i = 0; i < cleanup.length; i++) {
        clientList.splice(clientList.indexOf(cleanup[i]), 1);
    }

}

chatServer.listen(9000);

最佳答案

你这里有错字


  
    if(clientList [i] .writeable)
  


该属性的正确名称是可写的

10-06 06:11