vue h5开发,ios锁屏后websocket断开
<p>h5项目开发中,出现ios在手机锁屏后websocket连接断掉的问题,解决方法是在mounted生命周期中监听页面呼出事件,在页面呼出状态中判断websocket的连接状态,当websocket处于未连接状态时,重新连接websocket</p><div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">mounted () {
document.addEventListener(</span>'visibilitychange', () =><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (!document.hidden) {<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">页面呼出</span>
<span style="color: rgba(0, 0, 255, 1)">if</span> (<span style="color: rgba(0, 0, 255, 1)">this</span>.websocket.readyState === 2 ||
<span style="color: rgba(0, 0, 255, 1)">this</span>.websocket.readyState === 3<span style="color: rgba(0, 0, 0, 1)">) {
</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.createWebSocket()
}
}
})
}</span></pre>
</div>
<p>websocket状态如下:</p>
<p>根据readyState属性可以判断webSocket的连接状态,该属性的值可以是下面几种:<br>0 :对应常量CONNECTING (numeric value 0),<br> 正在建立连接连接,还没有完成。The connection has not yet been established.<br>1 :对应常量OPEN (numeric value 1),<br> 连接成功建立,可以进行通信。The WebSocket connection is established and communication is possible.<br>2 :对应常量CLOSING (numeric value 2)<br> 连接正在进行关闭握手,即将关闭。The connection is going through the closing handshake.<br>3 : 对应常量CLOSED (numeric value 3)<br> 连接已经关闭或者根本没有建立。The connection has been closed or could not be opened.</p><br><br>
来源:https://www.cnblogs.com/sumyn/p/13321662.html
頁:
[1]