查看: 35|回复: 0

vue h5开发,ios锁屏后websocket断开

[复制链接]

1

主题

0

回帖

0

积分

热心网友

金币
0
阅读权限
220
精华
0
威望
0
贡献
0
在线时间
0 小时
注册时间
2011-9-16
发表于 2020-7-16 11:20:00 | 显示全部楼层 |阅读模式

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.



来源:https://www.cnblogs.com/sumyn/p/13321662.html
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

相关侵权、举报、投诉及建议等,请发 E-mail:qiongdian@foxmail.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部