当用户尝试向服务器写入任何内容时,我无法理解错误的原因:
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);