0

当用户尝试向服务器写入任何内容时,我无法理解错误的原因:

TypeError: Object #<identifyClient> has no method 'write'
at writeToAll (/root/node/mud/server.js:13:15)

为什么 identifyClient() 抱怨 write(),而它发生在 writeToAll() 中(第 15 行旁边有注释)?identifyClient() 实际上只设置客户端的名称,不应该关心 writeToAll() 中发生的情况。

var net = require("net");

var clients = [];

function identifyClient(client) {
    this.name = null;
    this.client = client;
}

function writeToAll(data, client) {
    for (var i = 0; i < clients.length; i++) {
        if (clients[i] != client) {
            clients[i].write(data); // This is line 15
        }
    }
}

var server = net.createServer(function(client) {
    var clientID = new identifyClient(client);
    clients.push(clientID);

    client.on("data", function(data) {
        writeToAll(data, client);
    });
});

server.listen(4444);
4

1 回答 1

2

替换clients[i]clients[i].client

clients一旦断开连接,您还必须从阵列中删除客户端。

于 2012-05-09T21:34:56.770 回答