国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Vue 項(xiàng)目中使用WebSocket 消息推送

這篇具有很好參考價(jià)值的文章主要介紹了Vue 項(xiàng)目中使用WebSocket 消息推送。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

一、功能需求

1.這是我在后臺(tái)管理項(xiàng)目中使用到的,主要的作用是搞一個(gè)消息提醒的功能。
2.主要有右上角的提示和有下角的消息彈框。
3.主要實(shí)現(xiàn)的功能是如果用戶有未讀的消息,那么首次登錄就彈框,如果用戶關(guān)閉了頁面,那么再次刷新頁面的時(shí)候,也不再彈框,意思就是一個(gè)賬戶沒有退出之前,也沒有實(shí)時(shí)消息推送的時(shí)候,只彈一次框。
4.如果用戶點(diǎn)擊了未讀消息,那么就會(huì)將此條消息置位歷史(已讀)。
頁面展示:
vue websocket消息推送,vue.js,websocket,前端

二、頁面代碼

備注:我的是后臺(tái)管理系統(tǒng)(用的是vue-element-admin),第一次寫websocket,所以我寫在了src->layout->AppMain.vue文件下面:文章來源地址http://www.zghlxwxcb.cn/news/detail-677104.html

<template>
  <section class="app-main">
    <Message-remind :message-list="messageList" />
    <transition name="fade-transform" mode="out-in">
      <keep-alive :include="cachedViews">
        <router-view :key="key" />
      </keep-alive>
    </transition>
  </section>
</template>

<script>
  import MessageRemind from '@/components/MessageRemind/index.vue'
  import { getToken, getSid } from "@/utils/auth"; // get token from cookie
  export default {
    name: 'AppMain',
    components: {
      MessageRemind
    },
    watch: {
      '$store.state.user': {
        handler: function (newValue, oldValue) {
          // 如果沒有token,則表明退出了登錄
          if (!newValue.token) {
            this.closeWebSocket();
          }
        },
        immediate: true,
        deep: true
      }
    },
    data() {
      return {
        // socket參數(shù)
        socket: null,
        timeout: 60 * 1000, // 45秒一次心跳
        timeoutObj: null, // 心跳心跳倒計(jì)時(shí)
        serverTimeoutObj: null, // 心跳倒計(jì)時(shí)
        timeoutnum: null, // 斷開 重連倒計(jì)時(shí)
        lockReconnect: false, // 防止
        websocket: null,

        messageList: {}
      };
    },
    created() {
      const hasToken = getToken();
      const sid = getSid();
      if (hasToken) {
        this.initWebSocket(hasToken, sid)
      }
    },
    computed: {
      cachedViews() {
        return this.$store.state.tagsView.cachedViews
      },
      key() {
        return this.$route.path
      }
    },
    mounted() {
      // console.log(this.$store.state.tagsView.cachedViews)
    },
    methods: {
      initWebSocket(token, sid) {
        // WebSocket與普通的請(qǐng)求所用協(xié)議有所不同,ws等同于http,wss等同于https
        this.websocket = new WebSocket(process.env.VUE_APP_WEB_SOCKET_BASE_API + '?uiticket=' + token + '&sid=' + sid);
        this.websocket.onopen = this.websocketonopen;
        this.websocket.onerror = this.websocketonerror;
        this.websocket.onmessage = this.setOnmessageMessage;
        this.websocket.onclose = this.websocketclose;
        // 監(jiān)聽窗口關(guān)閉事件,當(dāng)窗口關(guān)閉時(shí),主動(dòng)去關(guān)閉websocket連接,防止連接還沒斷開就關(guān)閉窗口,server端會(huì)拋異常。
        // window.onbeforeunload = that.onbeforeunload

      },

      start() {
        //清除延時(shí)器
        this.timeoutObj && clearTimeout(this.timeoutObj);
        this.serverTimeoutObj && clearTimeout(this.serverTimeoutObj);
        this.timeoutObj = setTimeout(() => {
          if (this.websocket && this.websocket.readyState == 1) {
            this.websocket.send('{"messageType": 99}');//發(fā)送消息,服務(wù)端返回信息,即表示連接良好,可以在socket的onmessage事件重置心跳機(jī)制函數(shù)
          } else {
            this.reconnect();
          }
          //定義一個(gè)延時(shí)器等待服務(wù)器響應(yīng),若超時(shí),則關(guān)閉連接,重新請(qǐng)求server建立socket連接
          this.serverTimeoutObj = setTimeout(() => {
            this.websocket.close();
          }, this.timeout)
        }, this.timeout)
      },
      reset() { // 重置心跳
        // 清除時(shí)間
        clearTimeout(this.timeoutObj);
        clearTimeout(this.serverTimeoutObj);
        // 重啟心跳
        this.start();
      },
      // 重新連接
      reconnect() {
        if (this.lockReconnect) return
        this.lockReconnect = true;
        //沒連接上會(huì)一直重連,設(shè)置延遲避免請(qǐng)求過多
        this.timeoutnum && clearTimeout(this.timeoutnum);
        this.timeoutnum = setTimeout(() => {
          this.initWebSocket();
          this.lockReconnect = false;
        }, 5000)
      },
      async setOnmessageMessage(event) {
        this.messageList = JSON.parse(event.data)
        if (this.messageList.data.messageType === 999) {
          this.websocket.send('{"messageType": 99}');
        }
        this.$store.dispatch('user/steMessageMenu', this.messageList)
        this.reset();
        // 自定義全局監(jiān)聽事件
        window.dispatchEvent(new CustomEvent('onmessageWS', {
          detail: {
            data: event.data
          }
        }))
        //發(fā)現(xiàn)消息進(jìn)入    開始處理前端觸發(fā)邏輯
        // if (event.data === 'success' || event.data === 'heartBath') return
      },
      websocketonopen(e) {
        // console.log('onopen', {e});
        //開啟心跳
        this.start();
        console.log("WebSocket連接成功!!!" + new Date() + "----" + this.websocket.readyState);
      },
      websocketonerror(e) {
        // console.log('websocketonerror', {e});
        console.log("WebSocket連接發(fā)生錯(cuò)誤" + e);
      },
      websocketclose(e) {
        this.websocket.close();
        clearTimeout(this.timeoutObj);
        clearTimeout(this.serverTimeoutObj);
        console.log("WebSocket連接關(guān)閉");
      },
      websocketsend(messsage) {
        this.websocket.send(messsage)
      },
      closeWebSocket() { // 關(guān)閉websocket
        this.websocket.close()
      },
    },
  }
</script>

<style lang="scss" scoped>
  @import "~@/styles/global-height.scss";

  .app-main {
    /* 50= navbar  50  */
    // min-height: calc(100vh - #{$navbar+'px'});
    width: 100%;
    position: relative;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    flex: 1;
  }

  .fixed-header+.app-main {
    padding-top: #{$navbar+'px'};
  }

  .hasTagsView {
    .app-main {
      // min-height: calc(100vh - #{$appMain+'px'});
    }

    .fixed-header+.app-main {
      padding-top: 90px;
    }
  }

  .copy {
    text-align: center;
    height: 30px;
    line-height: 30px;
    font-size: 13px;
    color: #666;
    background: #fff;
    width: 100%;
    box-shadow: 0 0 10px #dfe4ed;
  }
</style>

<style lang="scss">
  // fix css style bug in open el-dialog
  .el-popup-parent--hidden {
    .fixed-header {
      padding-right: 15px;
    }
  }
</style>

到了這里,關(guān)于Vue 項(xiàng)目中使用WebSocket 消息推送的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • Vue使用WebSocket實(shí)現(xiàn)實(shí)時(shí)獲取后端推送的數(shù)據(jù)。

    Vue可以使用WebSocket實(shí)現(xiàn)實(shí)時(shí)獲取后端推送的數(shù)據(jù)。 1.在Vue項(xiàng)目中安裝WebSocket庫 可以使用npm或yarn安裝WebSocket庫: 2.創(chuàng)建WebSocket連接 在Vue組件中創(chuàng)建WebSocket連接,連接到后端WebSocket服務(wù)器,代碼如下: 上面的代碼中,使用WebSocket連接到后端WebSocket服務(wù)器,通過監(jiān)聽onmessage事件,

    2024年02月08日
    瀏覽(26)
  • 記錄--你還在使用websocket實(shí)現(xiàn)實(shí)時(shí)消息推送嗎?

    記錄--你還在使用websocket實(shí)現(xiàn)實(shí)時(shí)消息推送嗎?

    在日常的開發(fā)中,我們經(jīng)常能碰見服務(wù)端需要主動(dòng)推送給客戶端數(shù)據(jù)的業(yè)務(wù)場(chǎng)景,比如數(shù)據(jù)大屏的實(shí)時(shí)數(shù)據(jù),比如消息中心的未讀消息,比如聊天功能等等。 本文主要介紹SSE的使用場(chǎng)景和如何使用SSE。 我們常規(guī)實(shí)現(xiàn)這些需求的方案有以下三種 輪詢 websocket SSE 在很久很久以前

    2024年02月19日
    瀏覽(25)
  • 使用GoEasy快速實(shí)現(xiàn)Android原生app中的websocket消息推送

    摘要: GoEasy帶來了一項(xiàng)令開發(fā)者振奮的消息:全面支持Android原生平臺(tái)!現(xiàn)在,您可以在Android應(yīng)用中使用最酷炫的實(shí)時(shí)通信功能,借助GoEasy輕松實(shí)現(xiàn)消息的發(fā)送和接收。本文將帶您領(lǐng)略GoEasy最新版本的威力,為您的應(yīng)用增添一抹鮮活的互動(dòng)色彩。 嗨,開發(fā)者朋友們!是時(shí)候展

    2024年02月12日
    瀏覽(26)
  • vue項(xiàng)目中如何使用websocket(步驟)

    WebSocket是一種在單個(gè)TCP連接上進(jìn)行全雙工通信的協(xié)議。Vue是一種流行的JavaScript框架,用于構(gòu)建用戶界面。 結(jié)合WebSocket和Vue,可以實(shí)現(xiàn)實(shí)時(shí)數(shù)據(jù)通信和動(dòng)態(tài)更新UI的效果 。 在本教程中,我們將學(xué)習(xí)如何在Vue應(yīng)用程序中使用WebSocket。 步驟1:安裝WebSocket庫 使用npm安裝WebSocket庫。

    2024年02月08日
    瀏覽(22)
  • SpringBoot+Netty+Vue+Websocket實(shí)現(xiàn)在線推送/聊天系統(tǒng)

    SpringBoot+Netty+Vue+Websocket實(shí)現(xiàn)在線推送/聊天系統(tǒng)

    ok,那么今天的話也是帶來這個(gè)非常常用的一個(gè)技術(shù),那就是咱們完成nutty的一個(gè)應(yīng)用,今天的話,我會(huì)介紹地很詳細(xì),這樣的話,拿到這個(gè)博文的代碼就基本上可以按照自己的想法去構(gòu)建自己的一個(gè)在線應(yīng)用了。比如聊天,在線消息推送之類的。其實(shí)一開始我原來的想法做在

    2024年02月03日
    瀏覽(26)
  • vue項(xiàng)目中使用websocket連接后立馬斷開(websocket連接后瞬間斷開)

    vue項(xiàng)目中使用websocket連接后立馬斷開(websocket連接后瞬間斷開)

    問題原因(連接后斷連的原因):前端給后端傳遞Authourization(token驗(yàn)證)時(shí),后端需要接收處理并設(shè)置響應(yīng)標(biāo)頭,不然就容易出現(xiàn)上敘錯(cuò)誤; 解決方法: 1、傳遞參數(shù)和驗(yàn)證權(quán)限; 2、后端處理后前端收到的響應(yīng)標(biāo)頭; ?然后,連接后斷連的問題就解決啦!感覺有用,就一鍵

    2024年02月11日
    瀏覽(25)
  • WebSocket服務(wù)端數(shù)據(jù)推送及心跳機(jī)制(Spring Boot + VUE)

    WebSocket服務(wù)端數(shù)據(jù)推送及心跳機(jī)制(Spring Boot + VUE)

    一、WebSocket簡(jiǎn)介 HTML5規(guī)范在傳統(tǒng)的web交互基礎(chǔ)上為我們帶來了眾多的新特性,隨著web技術(shù)被廣泛用于web APP的開發(fā),這些新特性得以推廣和使用,而websocket作為一種新的web通信技術(shù)具有巨大意義。WebSocket是HTML5新增的協(xié)議,它的目的是在瀏覽器和服務(wù)器之間建立一個(gè)不受限的雙

    2024年02月12日
    瀏覽(25)
  • 一張圖解決vue中websocket推送數(shù)過多導(dǎo)致頁面卡主(思路)

    一張圖解決vue中websocket推送數(shù)過多導(dǎo)致頁面卡主(思路)

    首先,websocket連接就不過多贅述了,主要講述連接以后出現(xiàn)的問題,這個(gè)問題點(diǎn)就在于渲染,websocket推送在數(shù)據(jù)量過大時(shí)不能一條一條渲染,這樣會(huì)導(dǎo)致瀏覽器壓力過大而崩潰(卡死),所以主要思路就是將數(shù)據(jù)緩存在一個(gè)數(shù)組中,通過定時(shí)器定時(shí)渲染數(shù)據(jù)。 下面是邏輯代碼

    2024年02月11日
    瀏覽(18)
  • vue項(xiàng)目上線之后出現(xiàn)WebSocketClient.js:13 WebSocket connection to ‘ws://localhost:8081/ws‘ failed:

    vue項(xiàng)目上線之后出現(xiàn)WebSocketClient.js:13 WebSocket connection to ‘ws://localhost:8081/ws‘ failed:

    ?原因就是開發(fā)環(huán)境與生產(chǎn)環(huán)境的區(qū)別,解決方法:如果沒使用過webscoket的話,禁用它就好了

    2024年02月08日
    瀏覽(25)
  • vue3項(xiàng)目使用WebSocket 傳輸 Protobuf 格式的數(shù)據(jù)

    vue3項(xiàng)目使用WebSocket 傳輸 Protobuf 格式的數(shù)據(jù)

    前端和后端數(shù)據(jù)傳輸常用數(shù)據(jù)格式: JSON(JavaScript Object Notation):與 HTTP 協(xié)議和 REST API 配合使用時(shí),JSON 數(shù)據(jù)是最常用的數(shù)據(jù)格式之一。對(duì)于 WebSocket,JSON 數(shù)據(jù)同樣適用。客戶端可以將消息轉(zhuǎn)換為 JSON 對(duì)象,并將其發(fā)送到服務(wù)器進(jìn)行處理,在服務(wù)器上生成響應(yīng)并返回給客戶端

    2024年02月10日
    瀏覽(27)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包