移动设备上的socket.io-在设备睡眠时不会断开连接,关闭浏览器,更改选项卡 [英] socket.io on mobile - does not disconnect on device sleep, close browser, change tab

查看:230
本文介绍了移动设备上的socket.io-在设备睡眠时不会断开连接,关闭浏览器,更改选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用NodeJS和Express服务器的基本Socket.io实现. (基本上是Socket.io网站上的入门"示例)

I have a basic Socket.io implementation using NodeJS with Express server. (Basically the Get Started example on Socket.io web site)

在桌面上,它按预期连接/断开连接.当您浏览到该页面时,套接字将连接.客户端和服务器端都会触发连接事件.用户关闭浏览器,套接字断开连接.

On desktop it connects/disconnects as expected. When you browse to the page the socket connects. Connection events are fired both on the client and server side. And she the user closes the browser, the socket disconnects.

但是在移动设备上,断开连接无法正常工作.当您浏览到该页面时,套接字将连接,并且将触发事件.但是,如果用户使用,则socket.io不会断开连接

But on mobile the disconnect does not work as expected. When you browse to the page the socket connects, and events are fired. But the socket.io does not disconnect if the user

  • 关闭浏览器应用(Safari或Android浏览器)(按主屏幕按钮)
  • 切换到另一个应用
  • 打开一个新标签页
  • 使设备进入睡眠状态(按睡眠"按钮)

并且服务器和客户端上均不会触发断开连接事件.

And the disconnect event is not fired on the server nor the client.

我试图使用window.pagehide通过JavaScript检测到这些事件,但是它也无法正确触发.

I tried to detect those events via JavaScript using window.pagehide but it is also not firing properly.

有没有人知道如何确保当用户离开移动设备页面时Socket.io断开连接.

Does any one have any idea how to make sure the Socket.io disconnects when the user walks away from the page on mobile.

任何帮助,不胜感激. 多鲁克

Any help much appreciated. Doruk

推荐答案

经过一番研究,我发现了一个不错的JS库,用于检测页面是否仍然可见.如果用户走开;甚至页面闲置了一段时间.

After some research I found a nice JS library detecting if the page is still visible; if the user walks away; or even if the page is idle for a certain period.

该库为ifvisible.js( https://github.com/serkanyersen/ifvisible.js/)

The library is ifvisible.js (https://github.com/serkanyersen/ifvisible.js/)

它检测到以下情况

  • 关闭浏览器应用
  • 切换到浏览器应用程序中的另一个标签
  • 切换到另一个应用
  • 将设备置于睡眠状态(*)

它们都可以在iOS和Android上使用,但Android上的最后一项除外.在某些较旧的Android设备上,未检测到此事件.我使用的是长时间的空闲超时.

They all work on both iOS and Android, with an exception of the last item on Android. On some older Android devices this event is not detected. I am using an long idle timeout instead.

因此,在这些事件的帮助下,我手动断开了套接字.

So with the help of these events I disconnect the socket manually.

这篇关于移动设备上的socket.io-在设备睡眠时不会断开连接,关闭浏览器,更改选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆