数据只返回输入的一个字符(一次)! 例如:你好
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);