为什么聊天客户只打印一个字符?

时间:2014-02-01 10:20:21

标签: javascript node.js chat

数据只返回输入的一个字符(一次)! 例如:你好

127.0.0.1:8884 says h 
127.0.0.1:8884 says e
127.0.0.1:8884 says l
127.0.0.1:8884 says l
127.0.0.1:8884 says o 

以下是代码:

var net = require('net');
var chatServer = net.createServer();
clinetList = [];

chatServer.on("connection",function(client){

   client.name = client.remoteAddress+":"+client.remotePort;
   client.write("Hi "+client.name+"!\n");

   clinetList.push(client);

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

function broadcast(message, client){
    for(var i=0; i<clinetList.length; i++){
        if(client !== clinetList[i]){
            clinetList[i].write(" "+client.name +"says "+ message+" ");
        }

    }
 }
chatServer.listen(9000);

0 个答案:

没有答案