基于node+vue實(shí)現(xiàn)簡(jiǎn)單的WebSocket聊天功能
首先,我需要用到node的nodejs-websocket模塊
使用yarn進(jìn)行安裝
yarn add nodejs-websocket --save
當(dāng)然,你也可以用npm進(jìn)行安裝
npm i nodejs-websocket --save
安裝完畢之后,我們開(kāi)始寫(xiě)服務(wù)端的代碼,首先,我用node在本地起了一個(gè)node服務(wù)器用來(lái)開(kāi)啟websocket服務(wù)
sock.js:
let ws = require("nodejs-websocket"); console.log("開(kāi)始建立鏈接"); ws.createServer(function (conn) { conn.on("text", function (str) { console.log("收到的信息為", str); conn.send(`${str}(機(jī)器人`) }); conn.on("close", function (code, reason) { console.log("關(guān)閉連接") }); conn.on("error", function (code, reason) { console.log("異常關(guān)閉") }) }).listen(8001); console.log("鏈接建立完畢");
服務(wù)端主要是用nodejs-websocket用來(lái)開(kāi)啟服務(wù),以及返回前端需要的值,這里我只是做了一個(gè)簡(jiǎn)單的處理,在接受值得后面加了一個(gè)‘機(jī)器人'的string,
然后,我們需要開(kāi)啟這個(gè)node服務(wù),
命令后面的路徑一定要找對(duì),我是把sock.js放在了根目錄的socket文件夾下面
執(zhí)行
yarn socket
最后,看我們的客戶端,客戶端我是想有一個(gè)輸入框,然后有個(gè)聊天框:
<template> <div class="test3"> <div class="msg" ref="box"> <div v-for="item in list" :class="[item.type,'msg-item']"> <p> {{item.content}} </p> </div> </div> <div class="input-group"> <input type="text" v-model="contentText"> <button @click="sendText">發(fā)送</button> </div> </div> </template> <script> export default { name: "index3", data() { return { list: [],//聊天記錄的數(shù)組 contentText: "",//input輸入的值 } }, methods: { //發(fā)送聊天信息 sendText() { let that = this; this.list = [...this.list, {type: "mine", content: this.contentText}];//通過(guò)type字段進(jìn)行區(qū)分是自己(mine)發(fā)的還是系統(tǒng)(robot)返回的 this.backText(function () { that.contentText = "";//加回調(diào)在得到返回?cái)?shù)據(jù)的時(shí)候清除輸入框的內(nèi)容 }); }, backText(callback) { let that = this; if (window.WebSocket) { let ws = new WebSocket("ws://192.168.11.169:8001"); ws.onopen = function (e) { console.log("鏈接服務(wù)器成功"); console.log("that.contentText is", that.contentText); ws.send(that.contentText); callback(); }; ws.onclose = function (e) { console.log("服務(wù)器關(guān)閉") }; ws.onerror = function () { console.log("服務(wù)器出錯(cuò)") }; ws.onmessage = function (e) { that.list = [...that.list, {type: "robot", content: e.data}] } } } }, watch: { //監(jiān)聽(tīng)list,當(dāng)有修改的時(shí)候進(jìn)行div的屏幕滾動(dòng),確保能看到最新的聊天 list: function () { let that = this; setTimeout(() => { that.$refs.box.scrollTop = that.$refs.box.scrollHeight; }, 0); //加setTimeout的原因:由于vue采用虛擬dom,我每次生成新的消息時(shí)獲取到的div的scrollHeight的值是生成新消息之前的值,所以造成每次都是最新的那條消息被隱藏掉了 } }, mounted() { } }; </script> <style scoped lang="scss"> .test3 { text-align: center; } .msg { width: 100px; height: 100px; overflow: auto; padding-top: 5px; border: 1px solid red; display: inline-block; margin-bottom: 6px; .msg-item { position: relative; overflow: hidden; p { display: inline-block; border-radius: 40px; background: #3C3D5A; color: white; float: left; padding: 2px 12px; margin: 0 0 2px 0; max-width: 70%; text-align: left; box-sizing: border-box; } &.mine { p { float: right; background: aquamarine; color: white; } } } } </style>
看一下最終效果:
- Vue+Websocket簡(jiǎn)單實(shí)現(xiàn)聊天功能
- vue使用WebSocket模擬實(shí)現(xiàn)聊天功能
- websocket+Vuex實(shí)現(xiàn)一個(gè)實(shí)時(shí)聊天軟件
- Vue +WebSocket + WaveSurferJS 實(shí)現(xiàn)H5聊天對(duì)話交互的實(shí)例
- 基于vue和websocket的多人在線聊天室
- vue+web端仿微信網(wǎng)頁(yè)版聊天室功能
- Vue.js仿微信聊天窗口展示組件功能
- vue + socket.io實(shí)現(xiàn)一個(gè)簡(jiǎn)易聊天室示例代碼
- vue實(shí)現(xiàn)websocket客服聊天功能
相關(guān)文章
使用proxytable 配置解決 vue-cli 的跨域請(qǐng)求問(wèn)題【推薦】
這篇文章主要介紹了利用 proxytable 配置解決 vue-cli 的跨域請(qǐng)求問(wèn)題,本文的目錄結(jié)構(gòu)基于 webpack 模板結(jié)構(gòu),需要的朋友可以參考下2018-05-05vue組件文檔(.md)中如何自動(dòng)導(dǎo)入示例(.vue)詳解
這篇文章主要給大家介紹了關(guān)于vue組件文檔(.md)中如何自動(dòng)導(dǎo)入示例(.vue)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01webpack&webpack-cli完全卸載過(guò)程
本文介紹了如何刪除全局和本地的webpack及其CLI,并提供了檢查webpack殘余文件的方法,總結(jié)了個(gè)人的操作經(jīng)驗(yàn),旨在為讀者提供參考,并期待獲得更多支持2024-09-09使用typescript構(gòu)建Vue應(yīng)用的實(shí)現(xiàn)
這篇文章主要介紹了使用typescript構(gòu)建Vue應(yīng)用的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08使用vue.js2.0 + ElementUI開(kāi)發(fā)后臺(tái)管理系統(tǒng)詳細(xì)教程(二)
這篇文章主要介紹了使用vue.js2.0 + ElementUI開(kāi)發(fā)后臺(tái)管理系統(tǒng)詳細(xì)教程(二),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-01-01