vue項(xiàng)目中使用websocket的實(shí)現(xiàn)
什么是websocket?
“WebSocket 是 HTML5 開(kāi)始提供的一種在單個(gè) TCP 連接上進(jìn)行全雙工通訊的協(xié)議。
WebSocket 使得客戶(hù)端和服務(wù)器之間的數(shù)據(jù)交換變得更加簡(jiǎn)單,允許服務(wù)端主動(dòng)向客戶(hù)端推送數(shù)據(jù)。在 WebSocket API 中,瀏覽器和服務(wù)器只需要完成一次握手,兩者之間就直接可以創(chuàng)建持久性的連接,并進(jìn)行雙向數(shù)據(jù)傳輸。
在 WebSocket API 中,瀏覽器和服務(wù)器只需要做一個(gè)握手的動(dòng)作,然后,瀏覽器和服務(wù)器之間就形成了一條快速通道。兩者之間就直接可以數(shù)據(jù)互相傳送。”
1. 在utils下新建websocket.js文件
// import { showInfoMsg, showErrorMsg } from '@/utils/popInfo' import ElementUI from 'element-ui'; function initWebSocket(e) { console.log(e) const wsUri = WS_API + "/webSocket/" + e; this.socket = new WebSocket(wsUri)//這里面的this都指向vue this.socket.onerror = webSocketOnError; this.socket.onmessage = webSocketOnMessage; this.socket.onclose = closeWebsocket; } function webSocketOnError(e) { ElementUI.Notification({ title: '', message: "WebSocket連接發(fā)生錯(cuò)誤" + e, type: 'error', duration: 0, }); } function webSocketOnMessage(e) { const data = JSON.parse(e.data); console.log(data.msgType === "INFO", data.msgType === "INFO") if (data.msgType === "INFO") { ElementUI.Notification({ title: '', message: data.msg, type: 'success', duration: 3000, }); } else if (data.msgType === "ERROR") { ElementUI.Notification({ title: '', message: data.msg, type: 'error', duration: 0, }); } } // 關(guān)閉websiocket function closeWebsocket() { console.log('連接已關(guān)閉...') } function close() { this.socket.close() // 關(guān)閉 websocket this.socket.onclose = function (e) { console.log(e)//監(jiān)聽(tīng)關(guān)閉事件 console.log('關(guān)閉') } } function webSocketSend(agentData) { this.socket.send(agentData); } export default { initWebSocket, close }
如果想刷新重新鏈接websocket 可以在App.vue頁(yè)面里添加個(gè)鉤子函數(shù)
mounted() { //當(dāng)在任一路由頁(yè)面被刷新時(shí),便是根組件app被從新建立,此時(shí)能夠進(jìn)行webSocket重連 //從localStorage中獲取用戶(hù)信息,是登陸狀態(tài)則能夠進(jìn)行webSocket重連 let token = localStorage.getItem("token"); if (token) { // userMessage = JSON.parse(userMessage); this.$websocket.initWebSocket(token); } },
客戶(hù)端主動(dòng)關(guān)閉websocket 在關(guān)閉的地方觸發(fā)函數(shù)就可以
logout() { // localStorage.clear(); localStorage.removeItem("token"); this.$websocket.close(); this.$store.dispatch("LogOut").then(() => { location.reload(); }); },
注:$webSocket 是在main.js中全局注冊(cè)了websocket.js文件
到此這篇關(guān)于vue項(xiàng)目中使用websocket的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)vue使用websocket內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 一文詳解websocket在vue2中的封裝使用
- Vue項(xiàng)目中Websocket的使用實(shí)例
- 前端之vue3使用WebSocket的詳細(xì)步驟
- vue3.0中使用websocket,封裝到公共方法的實(shí)現(xiàn)
- vue3+ts+Vuex中使用websocket協(xié)議方式
- Vue項(xiàng)目使用Websocket大文件FileReader()切片上傳實(shí)例
- vue項(xiàng)目使用websocket連接問(wèn)題及解決
- Vue?websocket封裝實(shí)現(xiàn)方法詳解
- vue使用websocket概念及示例
- vue基于websocket實(shí)現(xiàn)智能聊天及吸附動(dòng)畫(huà)效果
- Flask使用SocketIO實(shí)現(xiàn)WebSocket與Vue進(jìn)行實(shí)時(shí)推送
- vue+flv.js+SpringBoot+websocket實(shí)現(xiàn)視頻監(jiān)控與回放功能
- vue 項(xiàng)目中使用websocket的正確姿勢(shì)
- vue實(shí)現(xiàn)websocket客服聊天功能
- Vue+Websocket簡(jiǎn)單實(shí)現(xiàn)聊天功能
- vue使用WebSocket模擬實(shí)現(xiàn)聊天功能
- websocket+Vuex實(shí)現(xiàn)一個(gè)實(shí)時(shí)聊天軟件
- 使用WebSocket+SpringBoot+Vue搭建簡(jiǎn)易網(wǎng)頁(yè)聊天室的實(shí)現(xiàn)代碼
相關(guān)文章
vue實(shí)現(xiàn)微信二次分享以及自定義分享的示例
這篇文章主要介紹了vue實(shí)現(xiàn)微信二次分享以及自定義分享的示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03一文詳解Vue3中使用ref獲取元素節(jié)點(diǎn)
這篇文章主要介紹了一文詳解Vue3中使用ref獲取元素節(jié)點(diǎn),本文介紹在vue3的setup中使用composition?API獲取元素節(jié)點(diǎn)的幾種方法,需要的朋友可以參考一下2022-07-07Vue3中defineEmits、defineProps?不用引入便直接用
這篇文章主要介紹了Vue3中defineEmits、defineProps?不用引入便直接用,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09vue-admin-template?動(dòng)態(tài)路由的實(shí)現(xiàn)示例
本文主要介紹了ue-admin-template動(dòng)態(tài)路由的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12vue3+vite+ts使用require.context問(wèn)題
這篇文章主要介紹了vue3+vite+ts使用require.context問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05vue項(xiàng)目中使用qrcodesjs2生成二維碼簡(jiǎn)單示例
最近項(xiàng)目中需生成二維碼,發(fā)現(xiàn)了很好用的插件qrcodesjs2,所以下面這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目中使用qrcodesjs2生成二維碼的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05Vue-cli中為單獨(dú)頁(yè)面設(shè)置背景色的實(shí)現(xiàn)方法
下面小編就為大家分享一篇Vue-cli中為單獨(dú)頁(yè)面設(shè)置背景色的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02