欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue2中使用SSE(服務(wù)器發(fā)送事件)原因分析

 更新時(shí)間:2023年10月17日 14:59:54   作者:如晴天似雨天~  
SSE是圍繞只讀Comet交互推出的API或者模式,SSE 支持短輪詢、長(zhǎng)輪詢和HTTP 流,而且能在斷開(kāi)連接時(shí)自動(dòng)確定何時(shí)重新連接,本文重點(diǎn)給大家介紹

SSE簡(jiǎn)介

SSE(Server-Sent Events,服務(wù)器發(fā)送事件)是圍繞只讀Comet 交互推出的API 或者模式。

SSE API允許網(wǎng)頁(yè)獲得來(lái)自服務(wù)器的更新,用于創(chuàng)建到服務(wù)器的單向連接,服務(wù)器通過(guò)這個(gè)連接可以發(fā)送任意數(shù)量的數(shù)據(jù)。服務(wù)器響應(yīng)的MIME類型必須是text/event-stream,而且是瀏覽器中的JavaScript API 能解析格式輸出。

SSE 支持短輪詢、長(zhǎng)輪詢和HTTP 流,而且能在斷開(kāi)連接時(shí)自動(dòng)確定何時(shí)重新連接。

使用原因

之前系統(tǒng)通知公告等信息是通過(guò)每隔一段時(shí)間調(diào)用接口來(lái)判斷是否有新的公告或通知,最開(kāi)始想到的是用websocket,但是這場(chǎng)景只需要服務(wù)端往客戶端發(fā)送消息,所以商量后決定使用SSE。

// 使用這個(gè)庫(kù)可以添加的請(qǐng)求頭(比如添加token)
import { EventSourcePolyfill } from "event-source-polyfill";
import { getToken } from '@/utils/user'
export default {
  data() {
    return {
      eventSource: null
    }
  },
  mounted() {
    this.createSSE()
  },
  methods: {
    createSSE(){
      if(window.EventSource){
        // 根據(jù)環(huán)境的不同,變更url
        const url = process.env.VUE_APP_MSG_SERVER_URL
        // 用戶userId
        const { userId } = this.$store.state.user
        this.eventSource = new EventSourcePolyfill(
          `${url}/sse/connect/${userId}`, {
          // 設(shè)置重連時(shí)間
          heartbeatTimeout: 60 * 60 * 1000,
          // 添加token
          headers: {
             'Authorization': `Bearer ${getToken()}`,
          },
        });
        this.eventSource.onopen = (e) => {
          console.log("已建立SSE連接~")
        }
        this.eventSource.onmessage = (e) => {
          console.log("已接受到消息:", e.data)
        }
        this.eventSource.onerror = (e) => {
          if (e.readyState == EventSource.CLOSED) {
            console.log("SSE連接關(guān)閉");
          } else if (this.eventSource.readyState == EventSource.CONNECTING) {
            console.log("SSE正在重連");
            //重新設(shè)置token
            this.eventSource.headers = {
              'Authorization': `Bearer ${getToken()}`
            };
          } else {
            console.log('error', e);
          }
        };
      } else {
        console.log("你的瀏覽器不支持SSE~")
      }
    },
    beforeDestroy() {
      if(this.eventSource){
          const { userId } = this.$store.state.user
          // 關(guān)閉SSE
          this.eventSource.close();
          // 通知后端關(guān)閉連接
          this.$API.system.msg.closeSse(userId)
          this.eventSource = null
          console.log("退出登錄或關(guān)閉瀏覽器,關(guān)閉SSE連接~")
       }
    },

在createSSE被調(diào)用后,這個(gè)請(qǐng)求會(huì)一直在pending狀態(tài)

直到服務(wù)端向客戶端發(fā)送消息,狀態(tài)才會(huì)改變

最后離開(kāi)時(shí)記得關(guān)閉連接

到此這篇關(guān)于在vue2中使用SSE(服務(wù)器發(fā)送事件)的文章就介紹到這了,更多相關(guān)vue2使用SSE內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Vue.js?的過(guò)濾器你了解多少

    Vue.js?的過(guò)濾器你了解多少

    這篇文章主要為大家詳細(xì)介紹了Vue.js?的過(guò)濾器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-02-02
  • 最新評(píng)論