javascript - Socket.io lost connection when phone locks - Stack Overflow

setInterval(function(){ socket.emit("stayalive", { "room": room });}, 5000);i got

setInterval(function(){ 
socket.emit("stayalive", { "room": room });
}, 5000);

i got a simple browser application with an interval function running on my phone. Im debugging with Chrome on my Nexus 4. My problem is now: When i lock the phone, the interval stops after about 5 min and then my phone is disconnected from the socket io server.

If i dont lock the screen the interval dont stop and the phone will not disconnect.

12:29:10 First interval

12:34:27 Last ining interval

12:35:52 Client disconnected from Server

Is this an android feature that after 5 mins every interval will be killed or something like that, when the phone is locked?

I'm running chrome on my nexus 4, but friends with other devices got the same problem.

setInterval(function(){ 
socket.emit("stayalive", { "room": room });
}, 5000);

i got a simple browser application with an interval function running on my phone. Im debugging with Chrome on my Nexus 4. My problem is now: When i lock the phone, the interval stops after about 5 min and then my phone is disconnected from the socket io server.

If i dont lock the screen the interval dont stop and the phone will not disconnect.

12:29:10 First interval

12:34:27 Last ining interval

12:35:52 Client disconnected from Server

Is this an android feature that after 5 mins every interval will be killed or something like that, when the phone is locked?

I'm running chrome on my nexus 4, but friends with other devices got the same problem.

Share Improve this question edited May 12, 2015 at 13:19 kenny asked May 12, 2015 at 13:00 kennykenny 1,77620 silver badges15 bronze badges 3
  • You might want to use a wakelock. More Information – Junaid Commented May 12, 2015 at 13:07
  • @Skynet - the OP has a browser application. Is there access to a wakelock from a browser? – jfriend00 Commented May 12, 2015 at 21:12
  • Agreed jfriend00, I misunderstood. – Junaid Commented May 13, 2015 at 4:51
Add a ment  | 

1 Answer 1

Reset to default 6

Every mobile device OS implements its own protection for the battery of the mobile device and this includes deciding how long various things can "run in the background". This is done to keep phone apps from draining the battery far too quickly.

Variants of Chrome all have limitations on how often they will run intervals (even desktop versions of Chrome limit interval timers on windows/tabs that are not in the foreground) and mobile variants have even further limitations on things like webSockets which may need regular keep-alive packets to keep the connection open. It sounds like you've found that Chrome chooses the phone lock as the time to clamp down on long running applications and prevent them from using battery. The theory is likely that the end-user can't be viewing the application if the phone is locked so that's deemed a decent tradeoff scheme in order to save the small, mobile battery.

As a pure web page application, there is not much you can do to prevent the browser from "managing" your app like it is to save battery. If you have a native application, then you have access to other OS services (such as a push service) that are more optimized for long running background notification.


One possible work-around is to determine when the device/app has been woken up and the user is again interacting with it and then immediately reconnect and get all the updates you may have missed while you were disconnected. If you do this in an automatic fashion, it may not slow the user down at all. If you want actual background notifications while the phone is locked, then you will probably have to use more native app features on the phone for things like a push service.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744705567a4589034.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信