电脑浏览器上没问题,但是手机上一旦锁屏后,就会停止发送心跳,然后就与服务器断开
<script>
var fromid = 98;
var count = 1;
var toid = 1484553;
var ws = new WebSocket("ws://esp8266.b.com:8282");
ws.onopen = function(){
setInterval(show,10000);//心跳发送
}
ws.onmessage = function(e){
var message = eval("("+e.data+")");
console.log(message);
switch (message.type){
case "init":
var bild = '{"type":"bind","fromid":"'+fromid+'"}';
ws.send(bild);
return;
}
}
mui.init({
swipeBack:true //启用右滑关闭功能
});
var isActive = document.getElementById("mySwitch").classList.contains("mui-active");
if(isActive){
console.log("打开状态");
}else{
console.log("关闭状态");
}
document.getElementById("mySwitch").addEventListener("toggle",function(event){
if(event.detail.isActive){
console.log("你启动了开关");
var message = '{"data":"on","type":"say","fromid":"'+fromid+'","toid":"'+toid+'"}';
ws.send(message);
}else{
console.log("你关闭了开关");
var message = '{"data":"off","type":"say","fromid":"'+fromid+'","toid":"'+toid+'"}';
ws.send(message);
}
})
function show(){
ws.send('{"type":"pong"}');
}
</script>
手机黑屏或者后台后app的网络连接就被关闭了,app也暂停了,所以不能发送心跳了。
监听下ws.onclose ,在里面重新发起连接重连下
请问这个问题解决了吗?
客户端做个断开重连,app即时通讯都要有这个机制