1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Node.js实现简单聊天服务器【node.js】

Node.js实现简单聊天服务器【node.js】

时间:2020-02-04 05:15:49

相关推荐

Node.js实现简单聊天服务器【node.js】

web前端|js教程

Node.js,聊天服务器

web前端-js教程

使用Nodejs是如此简单的实现了一个简单的聊天服务器

adb 源码修改,vscode中怎么中断运行,ubuntu串口输出,tomcat 服务点不了,乌龟爬虫灯,php商业源码,厦门seo推广运营商,asp培训网站,苹果cmsv10模板购买lzw

实现代码如下:

源码采集器,ubuntu语言咋设置,怎么看tomcat源码,爬虫系列pyquery,php面对对象,河北互联网seo推广如何进行lzw

var net = require( et); var chatServer = net.createServer(),clientList = []; chatServer.on("connection",function(client){ client.name = client.remoteAddress + ":" + client.remotePort; client.write("Hi! "+client.name+" \n"); clientList.push(client); client.on("data",function(data){ //数据发送给客户端 broadcast(data,client); // clientList[i].write(data); }); client.on("end",function(){ clientList.splice(clientList.indexOf(client),1); }); client.on("error",function(e){ console.log(e) });});chatServer.listen(9000) function broadcast(message,client){ var cleanup = []; for(var i=0;i<clientList.length;i++){ if(client != clientList[i]){if(clientList[i].writable){ clientList[i].write(client.name = "says:"+message);}else{ cleanup.push[clientList[i]]; clientList[i].destory();} } }}

使用过程就是:

简历源码,ubuntu怎么查看mac,爬行爬虫的视频,php strace,查seo情况lzw

启动js

node chat.js

连接方式:telnet

telnet 127.0.0.1 9000

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。