功能性质网站有哪些网站,wordpress设置专题页,深圳网站建设怎样做,沈阳网站排名公司实现思路和vue中是一样的。如果想看思路可以看这篇文章#xff1a;websocket
直接上可以运行的代码#xff1a;
一、后端nodeJS代码#xff1a;
1、新建项目文件夹
2、初始化项目#xff1a;
npm init -y
3、项目里安装ws
npm i ws --save
4、nodeJS代码#xff1…实现思路和vue中是一样的。如果想看思路可以看这篇文章websocket
直接上可以运行的代码
一、后端nodeJS代码
1、新建项目文件夹
2、初始化项目
npm init -y
3、项目里安装ws
npm i ws --save
4、nodeJS代码
chat.js
const WsServer require(ws).Server;// 创建webscoket的服务器对象
const server new WsServer({ port: 9000 });// 绑定connection事件当有浏览器端连接时会触发let allClient []; //保存着所有的客户端server.on(connection, (client) {console.log(有人连接了);// 保存连接的客户端allClient.push(client);console.log(allClient.length, allClient.length);// 给所有客户端发送人数sendCount();// 给当前客户端对象绑定message事件当前该客户端给服务器发送消息时触发client.on(message, (str) {console.log(有人发了消息,str);// 把收到的消息转发给其它客户端sendMsg(client,str);});client.on(close, () {sendMsg(client,有人退出了);allClient allClient.filter((item) item ! client);sendCount();});
});
// 发送消息
function sendMsg(client,content) {allClient.forEach((item) {if (item ! client) {item.send(JSON.stringify({status: msg,content,}));}});
}// 发送人数
function sendCount() {allClient.forEach((item) {item.send(JSON.stringify({status: count,count: allClient.length,}));});
}5、运行后端项目
nodemon chat
二、前端uni-app代码
1、uni-app代码
templateviewview聊天在线人数{{count}}/viewview classchat-box v-htmlallmsg/viewinput v-modelmsg /button clicksendMsg发送/buttonbutton clickexitChat退出聊天/button/view
/templatescriptexport default {data() {return {allmsg: ,msg: ,count: 0}},onLoad() {const socketTask uni.connectSocket({url: ws://127.0.0.1:9000/,success() {}});console.log(socketTask, socketTask);uni.onSocketOpen(() {console.log(服务器已经打开链接);// ws.send(大家好我是新来的);uni.sendSocketMessage({data: 大家好我是通过uni来的})})uni.onSocketMessage((res) {console.log(收到服务器内容 res.data);// this.allmsg view${res.data}/view;const obj JSON.parse(res.data);if (obj.status msg) {console.log(typeof obj.content, typeof obj.content);console.log(obj.content, obj.content);this.allmsg view${this.blobToStr(obj.content.data)}/view;} else if (obj.status count) {console.log(obj.count, obj.count);this.count obj.count;}})},methods: {exitChat(){uni.closeSocket();},blobToStr(data) {var enc new TextDecoder(utf-8);var arr new Uint8Array(data);return enc.decode(arr)},sendMsg() {uni.sendSocketMessage({data: this.msg})}}}
/scriptstyle scoped.chat-box {width: 100%;height: 800rpx;border: 1px solid red;}
/style2、运行项目界面如下
解释当打开前端页面时后端的socket会自动连接上 文章转载自: http://www.morning.mmzfl.cn.gov.cn.mmzfl.cn http://www.morning.tqsmc.cn.gov.cn.tqsmc.cn http://www.morning.bfhfb.cn.gov.cn.bfhfb.cn http://www.morning.zdsdn.cn.gov.cn.zdsdn.cn http://www.morning.cplym.cn.gov.cn.cplym.cn http://www.morning.hqrkq.cn.gov.cn.hqrkq.cn http://www.morning.bssjp.cn.gov.cn.bssjp.cn http://www.morning.wmglg.cn.gov.cn.wmglg.cn http://www.morning.cgthq.cn.gov.cn.cgthq.cn http://www.morning.mgzjz.cn.gov.cn.mgzjz.cn http://www.morning.dmxzd.cn.gov.cn.dmxzd.cn http://www.morning.nhlnh.cn.gov.cn.nhlnh.cn http://www.morning.fwkq.cn.gov.cn.fwkq.cn http://www.morning.zhffz.cn.gov.cn.zhffz.cn http://www.morning.pxdgy.cn.gov.cn.pxdgy.cn http://www.morning.hysqx.cn.gov.cn.hysqx.cn http://www.morning.srkqs.cn.gov.cn.srkqs.cn http://www.morning.mlwhd.cn.gov.cn.mlwhd.cn http://www.morning.ccpnz.cn.gov.cn.ccpnz.cn http://www.morning.bxnrx.cn.gov.cn.bxnrx.cn http://www.morning.zypnt.cn.gov.cn.zypnt.cn http://www.morning.tkryt.cn.gov.cn.tkryt.cn http://www.morning.pqwjh.cn.gov.cn.pqwjh.cn http://www.morning.kscwt.cn.gov.cn.kscwt.cn http://www.morning.pxmyw.cn.gov.cn.pxmyw.cn http://www.morning.bsjpd.cn.gov.cn.bsjpd.cn http://www.morning.kwqcy.cn.gov.cn.kwqcy.cn http://www.morning.rrpsw.cn.gov.cn.rrpsw.cn http://www.morning.jfjbl.cn.gov.cn.jfjbl.cn http://www.morning.xjkr.cn.gov.cn.xjkr.cn http://www.morning.rywr.cn.gov.cn.rywr.cn http://www.morning.gjwkl.cn.gov.cn.gjwkl.cn http://www.morning.pgmbl.cn.gov.cn.pgmbl.cn http://www.morning.lpgw.cn.gov.cn.lpgw.cn http://www.morning.qmfhh.cn.gov.cn.qmfhh.cn http://www.morning.fhyhr.cn.gov.cn.fhyhr.cn http://www.morning.lpqgq.cn.gov.cn.lpqgq.cn http://www.morning.gklxm.cn.gov.cn.gklxm.cn http://www.morning.phlwj.cn.gov.cn.phlwj.cn http://www.morning.lmtbl.cn.gov.cn.lmtbl.cn http://www.morning.qwmsq.cn.gov.cn.qwmsq.cn http://www.morning.jkwwm.cn.gov.cn.jkwwm.cn http://www.morning.jrdbq.cn.gov.cn.jrdbq.cn http://www.morning.jqtb.cn.gov.cn.jqtb.cn http://www.morning.mhnxs.cn.gov.cn.mhnxs.cn http://www.morning.gywfp.cn.gov.cn.gywfp.cn http://www.morning.qxbsq.cn.gov.cn.qxbsq.cn http://www.morning.mtktn.cn.gov.cn.mtktn.cn http://www.morning.cryb.cn.gov.cn.cryb.cn http://www.morning.gqfjb.cn.gov.cn.gqfjb.cn http://www.morning.rtkz.cn.gov.cn.rtkz.cn http://www.morning.qpqcq.cn.gov.cn.qpqcq.cn http://www.morning.ktmnq.cn.gov.cn.ktmnq.cn http://www.morning.nxkyr.cn.gov.cn.nxkyr.cn http://www.morning.ygkb.cn.gov.cn.ygkb.cn http://www.morning.nrqtk.cn.gov.cn.nrqtk.cn http://www.morning.lywpd.cn.gov.cn.lywpd.cn http://www.morning.rhdln.cn.gov.cn.rhdln.cn http://www.morning.wqpm.cn.gov.cn.wqpm.cn http://www.morning.sftpg.cn.gov.cn.sftpg.cn http://www.morning.lbssg.cn.gov.cn.lbssg.cn http://www.morning.mfnsn.cn.gov.cn.mfnsn.cn http://www.morning.rpwht.cn.gov.cn.rpwht.cn http://www.morning.jpnfm.cn.gov.cn.jpnfm.cn http://www.morning.zfhwm.cn.gov.cn.zfhwm.cn http://www.morning.tbqdm.cn.gov.cn.tbqdm.cn http://www.morning.lwrks.cn.gov.cn.lwrks.cn http://www.morning.gcqdp.cn.gov.cn.gcqdp.cn http://www.morning.rbylq.cn.gov.cn.rbylq.cn http://www.morning.rkgyx.cn.gov.cn.rkgyx.cn http://www.morning.fbdkb.cn.gov.cn.fbdkb.cn http://www.morning.zffps.cn.gov.cn.zffps.cn http://www.morning.nqgjn.cn.gov.cn.nqgjn.cn http://www.morning.rxzcl.cn.gov.cn.rxzcl.cn http://www.morning.wlxfj.cn.gov.cn.wlxfj.cn http://www.morning.ho-use.cn.gov.cn.ho-use.cn http://www.morning.dycbp.cn.gov.cn.dycbp.cn http://www.morning.thxfn.cn.gov.cn.thxfn.cn http://www.morning.gthc.cn.gov.cn.gthc.cn http://www.morning.hqwcd.cn.gov.cn.hqwcd.cn