Android websocket闪退,WebSocket 移动端连接会断开-坑

  • Post author:
  • Post category:其他


WebSocket在web端不会掉线;

WebSocket在移动端会有很多种场景导致连接会断开,所以需要实现一个websocket断开与重连的功能;

Websocket需要与后台实现一个ping,pong的心跳保持,用于监测服务器是否已经断开连接;

websocket建立

var host = getRootPath_dc().replace(“http”, “ws”)+”/mediashow/mediasocket”; //声明host注意:是ws协议

console.log(host);

function getRootPath_dc() {

var pathName = window.location.pathname.substring(1);

var webName = pathName == ” ? ” : pathName.substring(0, pathName.indexOf(‘/’));

if (webName == “”) {

return window.location.protocol + ‘//’ + window.location.host;

}

else {

return window.location.protocol + ‘//’ + window.location.host + ‘/’ + webName;

}

}

//声明一个对开连接,关闭socket方法</