我们理论知识太多还不如实战,我们来写一个可以通过telnet腾讯的小玩意玩玩:
var net = require('net'); var ChatServer = net.createServer(); clinetList = []; ChatServer.on("connection",function(client){ clinetList.push(client); client.write('hello world leigood '); client.on("data",function(data){ for(var i =0; i< clinetList.length; i++){ if(client != clinetList[i]){ clinetList[i].write(data.toString() ); } } }); }); ChatServer.listen(3000);
然后在cmd命令行里执行
telnet localhost 3000
即可实现简单的通讯了: