手机上的应用,包括浏览器退到后台,通常会被操作系统暂停. 似乎Android上的Chrome会打开websockets,但是当屏幕关闭时它会完全暂停计时器。这会导致设备在服务器端心跳超时时断开连接,并且由于心跳计时器未在客户端运行,它将继续重新连接并断开连接。类似保持连接的功能最好还是通过原生的服务实现更靠谱些.
这里有个后台表现列表供参考
Mobile devices can have different behaviour:
in background tabs
when display is turned off
This ticket is to track state of things
Timers at mobile devices
Browser
Background
Display off
Background WW
Display off WW
Android browser
OK
fail
OK
fail
Android FF
fail
fail
OK
OK
Android Chrome
OK
OK
OK
OK
iOS Chrome
OK
fail
OK
fail
iOS Safari
fail
fail
fail
fail
iOS Safari - sometimes timer in background can continue working 0-20 seconds
iOS Safari - foreground tab continue working 10-15 seconds after display turned off
Conclusion
Everything is bad on ios. The only solution is to reselect master.
On android things are acceptable, if you generate timer events from WebWorker
SharedWorker will not help:
not supported in browsers with timer problems
not much needed in browsers without timer problems
结论是 Chrome 还不错!