亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Vue通過配置WebSocket并實(shí)現(xiàn)群聊功能

 更新時(shí)間:2019年12月31日 09:02:21   作者:神奇的程序員  
本篇文章將與各位開發(fā)者分享下 vue-native-websocket 庫(kù)的使用以及配置,通過實(shí)例代碼給大家分享Vue通過配置WebSocket并實(shí)現(xiàn)群聊功能,需要的朋友可以參考下

原文鏈接:https://juejin.im/post/5e08d53a6fb9a0162b7f4bad

寫JQuery項(xiàng)目時(shí),使用websocket很簡(jiǎn)單,不用去考慮模塊化,組件之間的訪問問題,面向文檔編程即可,在Vue項(xiàng)目中使用時(shí),遠(yuǎn)遠(yuǎn)沒有想象中的那么簡(jiǎn)單,需要考慮很多場(chǎng)景,本篇文章將與各位開發(fā)者分享下 vue-native-websocket 庫(kù)的使用以及配置,用其實(shí)現(xiàn)群聊功能。先看下最終實(shí)現(xiàn)的效果

 

安裝依賴

本文中對(duì)于 vue-native-websocket 庫(kù)的講解,項(xiàng)目中配置了vuex,對(duì)其不了解的開發(fā)者請(qǐng)移步官方文檔,如果選擇繼續(xù)閱讀本篇文章會(huì)比較吃力。

vue-native-websocket安裝
# yarn | npm 安裝
yarn add vue-native-websocket | npm install vue-native-websocket --save

安裝成功

配置插件

在 main.js 中進(jìn)行導(dǎo)入

import VueNativeSock from 'vue-native-websocket'

使用 VueNativeSock 插件,并進(jìn)行相關(guān)配置

// main.js
// base.lkWebSocket為你服務(wù)端websocket地址
Vue.use(VueNativeSock,base.lkWebSocket,{
 // 啟用Vuex集成,store的值為你的vuex
 store: store,
 // 數(shù)據(jù)發(fā)送/接收使用使用json格式
 format: "json",
 // 開啟自動(dòng)重連
 reconnection: true,
 // 嘗試重連的次數(shù)
 reconnectionAttempts: 5,
 // 重連間隔時(shí)間
 reconnectionDelay: 3000,
 // 將數(shù)據(jù)進(jìn)行序列化,由于啟用了json格式的數(shù)據(jù)傳輸這里需要進(jìn)行重寫
 passToStoreHandler: function (eventName, event) {
 if (!eventName.startsWith('SOCKET_')) { return }
 let method = 'commit';
 let target = eventName.toUpperCase();
 let msg = event;
 if (this.format === 'json' && event.data) {
 msg = JSON.parse(event.data);
 if (msg.mutation) {
 target = [msg.namespace || '', msg.mutation].filter((e) => !!e).join('/');
 } else if (msg.action) {
 method = 'dispatch';
 target = [msg.namespace || '', msg.action].filter((e) => !!e).join('/');
 }
 }
 this.store[method](target, msg);
 this.store.state.socket.message = msg;
 }
});

vuex的相關(guān)配置:mutations和actions添加相關(guān)函數(shù)

// vuex配置文件
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex);

export default new Vuex.Store({
 state: {
 token:"",
 userID:"",
 // 用戶頭像
 profilePicture: "",
 socket: {
 // 連接狀態(tài)
 isConnected: false,
 // 消息內(nèi)容
 message: '',
 // 重新連接錯(cuò)誤
 reconnectError: false
 }
 },
 mutations: {
 SOCKET_ONOPEN (state, event) {
 // 連接打開觸發(fā)的函數(shù)
 Vue.prototype.$socket = event.currentTarget;
 state.socket.isConnected = true
 },
 SOCKET_ONCLOSE (state, event) {
 // 連接關(guān)閉觸發(fā)的函數(shù)
 state.socket.isConnected = false;
 console.log(event);
 },
 SOCKET_ONERROR (state, event) {
 // 連接發(fā)生錯(cuò)誤觸發(fā)的函數(shù)
 console.error(state, event)
 },
 SOCKET_ONMESSAGE (state, message) {
 // 收到消息時(shí)觸發(fā)的函數(shù)
 state.socket.message = message
 },
 SOCKET_RECONNECT(state, count) {
 // 重新連接觸發(fā)的函數(shù)
 console.info(state, count)
 },
 SOCKET_RECONNECT_ERROR(state) {
 // 重新連接失敗觸發(fā)的函數(shù)
 state.socket.reconnectError = true;
 },
 },
 actions: {
 customerAdded (context) {
 // 新連接添加函數(shù)
 console.log('action received: customerAdded');
 console.log(context)
 }
 },
 modules: {
 }
})

至此 vue-native-websocket 配置結(jié)束,如需了解更多配置方法,請(qǐng)移步 npm倉(cāng)庫(kù)

使用插件并實(shí)現(xiàn)群聊

在消息發(fā)送接收組件中添加 onmessage 監(jiān)聽(mounted生命周期中)

// 監(jiān)聽消息接收
this.$options.sockets.onmessage = (res)=>{
 // res.data為服務(wù)端返回的數(shù)據(jù)
 const data = JSON.parse(res.data);
 // 200為服務(wù)端連接建立成功時(shí)返回的狀態(tài)碼(此處根據(jù)真實(shí)后端返回值進(jìn)行相應(yīng)的修改)
 if(data.code===200){
 // 連接建立成功
 console.log(data.msg);
 }else{
 // 獲取服務(wù)端推送的消息
 const msgObj = {
 msg: data.msg,
 avatarSrc: data.avatarSrc,
 userID: data.userID
 };
 // 渲染頁(yè)面:如果msgArray存在則轉(zhuǎn)json
 if(lodash.isEmpty(localStorage.getItem("msgArray"))){
 this.renderPage([],msgObj,0);
 }else{
 this.renderPage(JSON.parse(localStorage.getItem("msgArray")),msgObj,0);
 }
 }
};

實(shí)現(xiàn)消息發(fā)送

// 消息發(fā)送函數(shù)
sendMessage: function (event) {
 if (event.keyCode === 13) {
 // 阻止編輯框默認(rèn)生成div事件
 event.preventDefault();
 let msgText = "";
 // 獲取輸入框下的所有子元素
 let allNodes = event.target.childNodes;
 for(let item of allNodes){
 // 判斷當(dāng)前元素是否為img元素
 if(item.nodeName==="IMG"){
 msgText += `/${item.alt}/`;
 }
 else{
 // 獲取text節(jié)點(diǎn)的值
 if(item.nodeValue!==null){
 msgText += item.nodeValue;
 }
 }
 }
 // 消息發(fā)送: 消息內(nèi)容、狀態(tài)碼、當(dāng)前登錄用戶的頭像地址、用戶id
 this.$socket.sendObj({msg: msgText,code: 0,avatarSrc: this.$store.state.profilePicture,userID: this.$store.state.userID});
 // 清空輸入框中的內(nèi)容
 event.target.innerHTML = "";
 }
}

實(shí)現(xiàn)頁(yè)面渲染

// 渲染頁(yè)面函數(shù)
renderPage: function(msgArray,msgObj,status){
 if(status===1){
 // 頁(yè)面第一次加載,如果本地存儲(chǔ)中有數(shù)據(jù)則渲染至頁(yè)面
 let msgArray = [];
 if(localStorage.getItem("msgArray")!==null){
 msgArray = JSON.parse(localStorage.getItem("msgArray"));
 for (let i = 0; i<msgArray.length;i++){
 const thisSenderMessageObj = {
 "msgText": msgArray[i].msg,
 "msgId": i,
 "avatarSrc": msgArray[i].avatarSrc,
 "userID": msgArray[i].userID
 };
 // 解析并渲染
 this.messageParsing(thisSenderMessageObj);
 }
 }
 }else{
 // 判斷本地存儲(chǔ)中是否有數(shù)據(jù)
 if(localStorage.getItem("msgArray")===null){
 // 新增記錄
 msgArray.push(msgObj);
 localStorage.setItem("msgArray",JSON.stringify(msgArray));
 for (let i = 0; i <msgArray.length; i++){
 const thisSenderMessageObj = {
 "msgText": msgArray[i].msg,
 "msgId": i,
 "avatarSrc": msgArray[i].avatarSrc,
 "userID": msgArray[i].userID,
 };
 // 解析并渲染
 this.messageParsing(thisSenderMessageObj);
 }
 }else{
 // 更新記錄
 msgArray = JSON.parse(localStorage.getItem("msgArray"));
 msgArray.push(msgObj);
 localStorage.setItem("msgArray",JSON.stringify(msgArray));
 const thisSenderMessageObj = {
 "msgText": msgObj.msg,
 "msgId": Date.now(),
 "avatarSrc": msgObj.avatarSrc,
 "userID": msgObj.userID
 };
 // 解析并渲染
 this.messageParsing(thisSenderMessageObj);
 }
 }
}

實(shí)現(xiàn)消息解析

// 消息解析
messageParsing: function(msgObj){
 // 解析接口返回的數(shù)據(jù)進(jìn)行渲染
 let separateReg = /(\/[^/]+\/)/g;
 let msgText = msgObj.msgText;
 let finalMsgText = "";
 // 將符合條件的字符串放到數(shù)組里
 const resultArray = msgText.match(separateReg);
 if(resultArray!==null){
 for (let item of resultArray){
 // 刪除字符串中的/符號(hào)
 item = item.replace(/\//g,"");
 for (let emojiItem of this.emojiList){
 // 判斷捕獲到的字符串與配置文件中的字符串是否相同
 if(emojiItem.info === item){
 const imgSrc = require(`../assets/img/emoji/${emojiItem.hover}`);
 const imgTag = `<img src="${imgSrc}" width="28" height="28" alt="${item}">`;
 // 替換匹配的字符串為img標(biāo)簽:全局替換
 msgText = msgText.replace(new RegExp(`/${item}/`,'g'),imgTag);
 }
 }
 }
 finalMsgText = msgText;
 }else{
 finalMsgText = msgText;
 }
 msgObj.msgText = finalMsgText;
 // 渲染頁(yè)面
 this.senderMessageList.push(msgObj);
 // 修改滾動(dòng)條位置
 this.$nextTick(function () {
 this.$refs.messagesContainer.scrollTop = this.$refs.messagesContainer.scrollHeight;
 });
}

DOM結(jié)構(gòu)

通過每條消息的userID和vuex中的存儲(chǔ)的當(dāng)前用戶的userID來(lái)判斷當(dāng)前消息是否為對(duì)方發(fā)送

<!--消息顯示-->
<div class="messages-panel" ref="messagesContainer">
 <div class="row-panel" v-for="item in senderMessageList" :key="item.msgId">
 <!--發(fā)送者消息樣式-->
 <div class="sender-panel" v-if="item.userID===userID">
 <!--消息-->
 <div class="msg-body">
 <!--消息尾巴-->
 <div class="tail-panel">
 <svg class="icon" aria-hidden="true">
 <use xlink:href="#icon-zbds30duihuakuangyou" rel="external nofollow" ></use>
 </svg>
 </div>
 <!--消息內(nèi)容-->
 <p v-html="item.msgText"/>
 </div>
 <!--頭像-->
 <div class="avatar-panel">
 <img :src="item.avatarSrc" alt="">
 </div>
 </div>
 <!--對(duì)方消息樣式-->
 <div class="otherSide-panel" v-else>
 <!--頭像-->
 <div class="avatar-panel">
 <img :src="item.avatarSrc" alt="">
 </div>
 <!--消息-->
 <div class="msg-body">
 <!--消息尾巴-->
 <div class="tail-panel">
 <svg class="icon" aria-hidden="true">
 <use xlink:href="#icon-zbds30duihuakuangzuo" rel="external nofollow" ></use>
 </svg>
 </div>
 <!--消息內(nèi)容-->
 <p v-html="item.msgText"/>
 </div>
 </div>
 </div>
</div>

總結(jié)

以上所述是小編給大家介紹的Vue通過配置WebSocket并實(shí)現(xiàn)群聊功能,希望對(duì)大家有所幫助!

相關(guān)文章

  • vue2.0多條件搜索組件使用詳解

    vue2.0多條件搜索組件使用詳解

    這篇文章主要為大家詳細(xì)介紹了vue2.0多條件搜索組件的實(shí)現(xiàn)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • VueCli生產(chǎn)環(huán)境打包部署跨域失敗的解決

    VueCli生產(chǎn)環(huán)境打包部署跨域失敗的解決

    這篇文章主要介紹了VueCli生產(chǎn)環(huán)境打包部署跨域失敗的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧
    2020-11-11
  • Vue中動(dòng)態(tài)綁定Ref的兩種方式總結(jié)

    Vue中動(dòng)態(tài)綁定Ref的兩種方式總結(jié)

    Vue3中的ref是一種創(chuàng)建響應(yīng)式引用的方式,它在Vue生態(tài)系統(tǒng)中扮演著重要角色,下面這篇文章主要給大家介紹了關(guān)于Vue中動(dòng)態(tài)綁定Ref的兩種方式,需要的朋友可以參考下
    2024-09-09
  • Vue源碼解析之?dāng)?shù)組變異的實(shí)現(xiàn)

    Vue源碼解析之?dāng)?shù)組變異的實(shí)現(xiàn)

    這篇文章主要介紹了Vue源碼解析之?dāng)?shù)組變異的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-12-12
  • 深入分析element ScrollBar滾動(dòng)組件源碼

    深入分析element ScrollBar滾動(dòng)組件源碼

    這篇文章主要介紹了element ScrollBar滾動(dòng)組件源碼深入分析,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧
    2019-01-01
  • Vue實(shí)現(xiàn)跑馬燈效果

    Vue實(shí)現(xiàn)跑馬燈效果

    這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)跑馬燈效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • 使用vue-element-admin框架調(diào)用后端接口及跨域的問題

    使用vue-element-admin框架調(diào)用后端接口及跨域的問題

    這篇文章主要介紹了使用vue-element-admin框架調(diào)用后端接口及跨域的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • vue組件常用的父子、兄弟、跨組件等傳值方法

    vue組件常用的父子、兄弟、跨組件等傳值方法

    Vue常用的三種傳值方式有:??父?jìng)髯??子傳父??非父子傳值?引用官網(wǎng)的一句話:父子組件的關(guān)系可以總結(jié)為?prop?向下傳遞,事件向上傳遞,熟悉vue各類關(guān)系的組件之間傳值方法會(huì)令開發(fā)更加得心應(yīng)手,下面將對(duì)父子、兄弟、頁(yè)級(jí)組件之間的傳值作淺談
    2023-12-12
  • Django+Vue實(shí)現(xiàn)WebSocket連接的示例代碼

    Django+Vue實(shí)現(xiàn)WebSocket連接的示例代碼

    這篇文章主要介紹了Django+Vue實(shí)現(xiàn)WebSocket連接的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • vue.js使用3DES加密的方法示例

    vue.js使用3DES加密的方法示例

    這篇文章主要介紹了vue.js使用3DES加密的方法,結(jié)合實(shí)例形式分析了vue.js使用3DES加密的具體操作步驟與使用技巧,并提供了CryptoJS-v3.1.2的本地下載,需要的朋友可以參考下
    2018-05-05

最新評(píng)論