我们来升级玩玩,废话不多说,代码如下所示:
/** * Created by leigood on 2016/8/12. */var net = require('net');var ChatSrever = net.createServer();clinetList = [];ChatSrever.on("connection",function(client){ client.name = client.remoteAddress + ":" + client.remotePort;//返回IP地址加上端口号 console.log(client.name + "leigood \n\r");//谁来了 clinetList.push(client); client.write('welcome to node server.......\n\r'); client.on("data",function(data){ for(var i =0; i< clinetList.length; i++){ if(client != clinetList[i]){ clinetList[i].write(client.name + ":" + data.toString() ); } } }); client.on("end",function(){ clinetList.splice(clinetList.indexOf(client),1); }); client.on("error",function(e){ console.log(e); })});ChatSrever.listen(4000);
在这里可以很明显的看到我们已经发生了相关的响应事件