为什么在 WebSockets 可用时使用 AJAX? [英] Why use AJAX when WebSockets is available?

查看:24
本文介绍了为什么在 WebSockets 可用时使用 AJAX?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 WebSockets 一段时间了,我选择使用 Node 服务器和 WebSockets 为我在大学的最后一年项目创建一个敏捷项目管理工具.我发现使用 WebSockets 使我的应用程序每秒可以处理的请求数量增加了 624%.

I've been using WebSockets for a while now, I have chosen to create an Agile project management tool for my final year project at University utilizing Node server and WebSockets. I found using WebSockets provided a 624% increase in the number of requests per second my application could process.

然而,自从开始这个项目以来,我已经阅读了安全漏洞,并且一些浏览器选择默认禁用 WebSockets..

However since starting the project I've read of security loopholes, and some browsers choosing to disable WebSockets by default..

这让我想到了一个问题:

当 WebSockets 似乎在降低延迟和资源开销方面做得如此出色时,为什么要使用 AJAX,AJAX 是否比 WebSockets 做得更好?

Why use AJAX when WebSockets seems to do such a great job of lowering latency and resource overhead, is there anything that AJAX does better than WebSockets?

推荐答案

WebSockets 并不是要取代 AJAX,甚至严格来说也不是 Comet/long-poll 的替代品(尽管在很多情况下这是有意义的).

WebSockets isn't intended to replace AJAX and is not strictly even a replacement for Comet/long-poll (although there are many cases where this makes sense).

WebSockets 的目的是在浏览器和服务器之间提供低延迟、双向、全双工和长时间运行的连接.WebSockets 为使用 HTTP 和 AJAX 无法真正实现的浏览器应用开辟了新的应用领域(交互式游戏、动态媒体流、与现有网络协议的桥接等).

The purpose of WebSockets is to provide a low-latency, bi-directional, full-duplex and long-running connection between a browser and server. WebSockets opens up new application domains to browser applications that were not really possible using HTTP and AJAX (interactive games, dynamic media streams, bridging to existing network protocols, etc).

然而,WebSockets 和 AJAX/Comet 在目的上肯定有重叠.例如,当浏览器想要收到服务器事件的通知(即推送)时,Comet 技术和 WebSockets 当然都是可行的选择.如果您的应用程序需要低延迟推送事件,那么这将是支持 WebSockets 的一个因素.另一方面,如果您需要与现有框架和已部署的技术(OAuth、RESTful API、代理、负载平衡器)共存,那么这将是支持 Comet 技术的一个因素(目前).

However, there is certainly an overlap in purpose between WebSockets and AJAX/Comet. For example, when the browser wants to be notified of server events (i.e. push) then Comet techniques and WebSockets are certainly both viable options. If your application needs low-latency push events then this would be a factor in favor of WebSockets. On the other hand, if you need to co-exist with existing frameworks and deployed technologies (OAuth, RESTful APIs, proxies, load balancers) then this would be a factor in favor of Comet techniques (for now).

如果您不需要 WebSockets 提供的特定优势,那么坚持使用现有技术(如 AJAX 和 Comet)可能是一个更好的主意,因为这允许您重用现有的庞大工具、技术生态系统并与之集成、安全机制、知识库(即,stackoverflow 上了解 HTTP/Ajax/Comet 的人比 WebSockets 多得多)等

If you don't need the specific benefits that WebSockets provides, then it's probably a better idea to stick with existing techniques like AJAX and Comet because this allows you to re-use and integrate with a huge existing ecosystem of tools, technologies, security mechanisms, knowledge bases (i.e. far more people on stackoverflow know HTTP/Ajax/Comet than WebSockets), etc.

另一方面,如果您要创建的新应用程序在 HTTP/Ajax/Comet 的延迟和连接限制范围内不能很好地工作,那么请考虑使用 WebSockets.

On the other hand, if you are creating a new application that just doesn't work well within the latency and connection constraints of HTTP/Ajax/Comet, then consider using WebSockets.

此外,一些答案表明 WebSockets 的缺点之一是服务器和浏览器支持有限/混合.让我稍微扩散一下.虽然 iOS(iPhone、iPad)仍然支持旧协议(Hixie),但大多数 WebSockets 服务器同时支持 Hixie 和 HyBi/IETF 6455 版本.大多数其他平台(如果它们还没有内置支持)可以通过 web 获得 WebSockets 支持-socket-js(基于 Flash 的 polyfill).这涵盖了绝大多数网络用户.此外,如果您使用 Node 作为服务器后端,那么请考虑使用 Socket.IO,其中包括 web-socket-js 作为一个回退,即使它不可用(或禁用),那么它将回退到使用给定浏览器可用的任何 Comet 技术.

Also, some answers indicate that one of the downsides of WebSockets is limited/mixed server and browser support. Let me just diffuse that a bit. While iOS (iPhone, iPad) still supports the older protocol (Hixie) most WebSockets servers support both Hixie and the HyBi/IETF 6455 version. Most other platforms (if they don't already have built-in support) can get WebSockets support via web-socket-js (Flash based polyfill). This covers the vast majority of web users. Also, if you are using Node for the server backend, then consider using Socket.IO which includes web-socket-js as a fallback and if even that is not available (or disabled) then it will fall back to using whatever Comet technique is available for the given browser.

更新:iOS 6 现在支持当前的 HyBi/IETF 6455 标准.

Update: iOS 6 now supports the current HyBi/IETF 6455 standard.

这篇关于为什么在 WebSockets 可用时使用 AJAX?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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