我有浏览器客户端 Javascript,它打开一个 WebSocket (使用 socket.io)来请求长时间运行的进程启动,然后在进程完成时获取回调。当我收到回调时,我会更新网页以让用户知道该过程已完成。
这工作正常,除了在我的 iPad 上,当我切换到另一个应用程序然后再回来时(它永远不会得到回调,因为我猜该应用程序当时不在线)。我假设在等待回调时处于睡眠状态的笔记本电脑或其他计算机上也会发生同样的事情。
是否有标准方法(或任何方法)来处理这种情况?谢谢。
作为参考,如果您想查看问题页面,请访问 http://amigen.perfectapi.com/
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.
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.
For reference, if you want to see the problem page, it is at http://amigen.perfectapi.com/
发布评论
评论(1)
在这种情况下,需要考虑以下几点:
检测应用程序是否离线
请参阅:
在线
和离线
事件当您的应用在计算机唤醒后检测到
在线
事件时,您可以获得任何您错过的信息。对于较旧的网络浏览器,您需要以更聪明的方式执行此操作。在 Pusher 中,我们在客户端和服务器之间添加了乒乓检查。如果客户端在一定时间内没有收到 ping 信号,则表明存在连接问题。如果服务器发送 ping 消息但在一定时间内未收到 pong 消息,则表明存在问题。
乒乓机制是 在规范中定义,但尚未在 WebSocket API 上定义发送 ping 或 pong 的方式。
获取丢失的信息
大多数实时服务器仅将消息传递给连接到的客户端。如果客户端未连接,可能是由于暂时的网络干扰或他们的计算机已经休眠了一段时间,那么这些客户端将错过该消息。
某些框架确实提供通过历史记录/缓存对消息的访问。对于那些不这样做的人,您需要检测问题(如上所述),然后获取所有丢失的消息。实现此目的的一个好方法是为每条消息提供时间戳或序列 ID,这样您就可以调用 Web 服务器说“给我自 X 以来的所有消息”。
There are a couple of things to consider in this scenario:
Detect the app going off/on line
See:
online
andoffline
eventsWhen your app detects the
online
event after the computer wakes up you can get any information that you've missed.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.
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.
Fetching missed information
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.
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".