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

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

问题描述

我一直在使用的WebSockets有一段时间了,我选择创建一个敏捷项目管理工具,我最后一年的项目,在大学利用节点服务器的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.

不过,由于开始我读过的安全漏洞的项目,以及一些浏览器选择默认。

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

这使我这样一个问题:

为什么要使用Ajax时的WebSockets似乎做降低延迟和资源开销的这样一个伟大的工作,有什么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和不严格,甚至替代彗星/长轮询(尽管有许多情况下,这是有意义的)。

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 /彗星的目的有重叠。例如,当浏览器想要的服务器事件(即推送),然后彗星技术和WebSockets的当然是两个可行的方案得到通知。如果你的应用需要低延迟推事件那么这将是赞成的WebSockets的一个因素。在另一方面,如果你需要共存与现有的框架和部署技术(OAuth的,基于REST的API,代理,负载平衡器),那么这将是赞成彗星技术的因素(现在)。

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和彗星的现有技术,因为这可以让你重新使用,用的工具,技术,庞大的现有生态系统的整合,安全机制,知识基础(上计算器即更多的人知道HTTP / AJAX /彗星比的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 /彗星的延迟和连接限制范围内正常工作,则可以考虑使用的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 版本。大多数其他平台(如果他们不这样做已经内置支持)可以得到的WebSockets通过网络插座JS (基于Flash的polyfill)。这包括网络用户的绝大多数。此外,如果您使用节点服务器后端,则可以考虑使用 Socket.IO 其中包括网络插座JS作为后备,如果甚至不可用(或禁用),那么它会回落到使用任何彗星技术可用于给定的浏览器。

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.

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

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