如何从正在睡觉的WebSocket客户端计算机或应用程序转到后台恢复(iPad上的Safari) [英] How do I recover from a WebSocket client computer going to sleep or app going to background (Safari on iPad)

查看:428
本文介绍了如何从正在睡觉的WebSocket客户端计算机或应用程序转到后台恢复(iPad上的Safari)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有浏览器客户端Javascript,它打开一个WebSocket(使用socket.io)来请求一个长时间运行的进程启动,然后在进程完成时获得一个回调。当我收到回叫时,我会更新网页,让用户知道该过程已经完成。

I have browser client Javascript which opens a WebSocket (using socket.io) to request a long-running process start, and then gets a callback when the process is done. When I get the callback, I update the web page to let the user know the process has completed.

这项工作正常,但我切换到另一个应用程序时我的iPad除外然后回来(它永远不会得到回调,因为我猜应用程序当时不在线)。我假设在等待回调时睡在笔记本电脑或其他计算机上会发生同样的事情。

This works ok, except on my iPad when I switch to another app and then come back (it never gets the callback, because I guess the app is not online at the time). I'm assuming the same thing will happen on a laptop or other computer that sleeps while waiting for the callback.

是否有标准的方式(或任何方式)来处理有这种情况?谢谢。

Is there a standard way (or any way) to deal with this scenario? Thanks.

作为参考,如果您想查看问题页面,请访问 http ://amigen.perfectapi.com/

For reference, if you want to see the problem page, it is at http://amigen.perfectapi.com/

推荐答案

在这种情况下需要考虑以下几点:

There are a couple of things to consider in this scenario:

检测应用程序何时开启

请参阅:在线和离线活动

当您的应用检测到时计算机唤醒后的 online 事件可以获得您错过的任何信息。

When your app detects the online event after the computer wakes up you can get any information that you've missed.

对于旧网站浏览器你需要以更聪明的方式做到这一点。在 Pusher ,我们在客户端和服务器之间添加了乒乓检查。如果客户端在一定时间内没有收到ping,则表示存在连接问题。如果服务器发送ping并且在某段时间内没有得到pong,则它知道存在问题。

For older web browsers you'll need to do this in a cleverer way. At Pusher we've added a ping - pong check between the client and server. If the client doesn't receive a ping within a certain amount of time it knows there's a connection problem. If the server sends a ping and doesn't get a pong back with a certain time it knows there's a problem.

乒乓机制是在规范中定义但是一种方式尚未在WebSocket API上定义发送ping或pong的信息。

A ping pong mechanism is defined in the spec but a way of sending a ping or pong hasn't been defined on the WebSocket API as yet.

获取遗漏的信息

大多数实时服务器仅向连接到客户端的消息传递。如果客户端没有连接,可能是由于临时网络干扰或者他们的计算机已经睡了一段时间,那么这些客户端将会错过该消息。

Most realtime servers only deliver messages to connected to clients. If a client isn't connected, maybe due to temporary network disturbance or their computer has been asleep for a while, then those clients will miss the message.

某些框架可以通过历史记录/缓存提供对消息的访问。对于那些不需要检测问题的人(如上所述),然后获取任何错过的消息。这样做的一个好方法是为每条消息提供一个时间戳或序列ID,这样你就可以打电话给你的网络服务器,说给我自X以来的所有消息。

Some frameworks do provide access to messages through a history/cache. For those that don't you'll need to detect the problem (as above) and then fetch any missed messages. A good way to do this is by providing a timestamp or sequence ID with each messages so you can make a call to your web server to say "give me all messages since X".

这篇关于如何从正在睡觉的WebSocket客户端计算机或应用程序转到后台恢复(iPad上的Safari)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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