Gmail 浏览器客户端如何检测互联网/服务器断开连接(速度和可扩展性)
我们有一个浏览器应用程序 (SaaS),我们希望在互联网连接或服务器连接丢失时通知用户。 Gmail 做得非常好,当我拔掉互联网电缆或禁用网络流量时,它立即说无法到达服务器,并给我一个重试倒计时。
实施这样的事情的最佳方法是什么?我是否希望客户端浏览器每秒向应用程序服务器发出 AJAX 请求,或者拥有一个仅报告“活动”的单独服务器。可扩展性将成为未来的一个问题。
We have an browser application (SaaS) where we would like to notify the user in the case of internet connection or server connection loss. Gmail does this very nicely, the moment I unplug the internet cable or disable network traffic it immediately says unable to reach the server and gives me a count down for retry.
What is the best way to implement something like this? Would I want the client browser issuing AJAX requests to the application server every second, or have a separate server that just reports back "alive". Scalability will be come an issue down the road.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于 GMail 已经每隔几秒检查一次新电子邮件,并且更频繁地检查聊天信息,因此无需单独请求即可判断连接是否已断开。如果您不使用 Ajax 进行其他类型的持续更新,那么是的,您只需让服务器回复某种“活动”信号即可。但请注意,由于 Ajax 跨域限制,您无法使用单独的服务器。
Because GMail already checks for new e-mails every some seconds and for chat information even more frequently, it can tell without a separate request if the connection is down. If you're not using Ajax for some other sort of constant update, then yes, you would just have your server reply with some sort of "alive" signal. Note that you couldn't use a separate server because of Ajax cross-domain restrictions, however.
当服务器向客户端报告(通过 Comet 推送)时,您必须为每个客户端维护一个开放的连接。如果您有大量客户,这可能会非常昂贵。正如您提到的,可扩展性可能是一个问题。另一种选择是轮询。您可以让它每 5-10 秒左右轮询一次,而不是每秒执行一次。
您还可以查看 Web Sockets (作为 HTML 5 的一部分开发),但我不确定它是否被广泛支持(据我所知只有 Chrome 支持)。
With the server reporting to the client (push via Comet), you have to maintain an open connection for each client. This can be pretty expensive if you have a large number of clients. Scalability can be an issue, as you mentioned. The other option is to poll. Instead of doing it every second, you can have it poll every 5-10 seconds or so.
Something else that you can look at is Web Sockets (developed as part of HTML 5), but I am not sure if it is widely supported (AFAIK only Chrome supports it).