vue h5開發,ios鎖屏后websocket斷開


h5項目開發中,出現ios在手機鎖屏后websocket連接斷掉的問題,解決方法是在mounted生命周期中監聽頁面呼出事件,在頁面呼出狀態中判斷websocket的連接狀態,當websocket處於未連接狀態時,重新連接websocket

mounted () {
    document.addEventListener('visibilitychange', () => {
      if (!document.hidden) {//頁面呼出
        if (this.websocket.readyState === 2 ||    
            this.websocket.readyState === 3) {
                this.createWebSocket()
          }
      }
    })
}

websocket狀態如下:

根據readyState屬性可以判斷webSocket的連接狀態,該屬性的值可以是下面幾種:
0 :對應常量CONNECTING (numeric value 0),
 正在建立連接連接,還沒有完成。The connection has not yet been established.
1 :對應常量OPEN (numeric value 1),
 連接成功建立,可以進行通信。The WebSocket connection is established and communication is possible.
2 :對應常量CLOSING (numeric value 2)
 連接正在進行關閉握手,即將關閉。The connection is going through the closing handshake.
3 : 對應常量CLOSED (numeric value 3)
 連接已經關閉或者根本沒有建立。The connection has been closed or could not be opened.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM