我对HTTP轮询,长轮询,HTTP流和WebSocket的理解 [英] My Understanding of HTTP Polling, Long Polling, HTTP Streaming and WebSockets

查看:474
本文介绍了我对HTTP轮询,长轮询,HTTP流和WebSocket的理解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SO和网络上阅读了许多有关我的问题标题中关键字的文章,并从中学到了很多.我读的一些问题与特定的实施挑战有关,而其他问题则与一般概念有关.我只想确保我理解所有概念以及为什么发明技术X而不是发明技术Y的理由等等.所以去了:

I have read many posts on SO and the web regarding the keywords in my question title and learned a lot from them. Some of the questions I read are related to specific implementation challenges while others focus on general concepts. I just want to make sure I understood all of the concepts and the reasoning why technology X was invented over technology Y and so on. So here goes:

Http轮询:基本上是AJAX,使用XmlHttpRequest.

Http Polling: Basically AJAX, using XmlHttpRequest.

Http Long Polling: AJAX,但服务器会保留响应,除非服务器进行了更新,一旦服务器进行了更新,它将发送该更新,然后客户端可以发送另一个请求.缺点是需要来回发送额外的标头数据,从而导致额外的开销.

Http Long Polling: AJAX but the server holds on to the response unless the server has an update, as soon as the server has an update, it sends it and then the client can send another request. Disadvantage is the additional header data that needs to be sent back and forth causing additional overhead.

Http流:与长轮询类似,但服务器以"Transfer Encoding:chunked"作为标头响应,因此,服务器每次发送一些数据时,我们都不需要发起新的请求(从而节省了额外的标头开销).这里的缺点是我们必须理解"并弄清楚数据的结构,以区分服务器发送的多个块.

Http Streaming: Similar to long polling but the server responds with a header with "Transfer Encoding: chunked" and hence we do not need to initiate a new request every time the server sends some data (and hence save the additional header overhead). The drawback here is that we have to "understand" and figure out the structure of the data to distinguish between multiple chunks sent by the server.

Java Applet,Flash,Silverlight::它们提供了通过tcp/ip连接到套接字服务器的功能,但是由于它们是插件,因此开发人员不想依赖它们.

Java Applet, Flash, Silverlight: They provide the ability to connect to socket servers over tcp/ip but since they are plugins, developers don't want to depend on them.

WebSockets :它们是新的API,它试图通过以下方式解决上述方法的不足:

WebSockets: they are the new API which tries to address the short comings of above methods in the following manner:

  • 相对于Java Applets,Flash或Silverlight等插件,WebSockets的唯一优势在于WebSockets内置于浏览器中,并且不依赖于插件.
  • 相对于HTTP流,WebSockets的唯一优势是您不必费力理解"并解析接收到的数据.
  • WebSockets相对于Long Polling的唯一优势是消除了额外的标头大小&根据需要打开和关闭套接字连接.

我还有其他重要的区别吗?很抱歉,如果我要重新提出或将关于SO的许多问题合并为一个问题,但是我只想从SO和Web上有关这些概念的所有信息中完全理解.

Are there any other significant differences that I am missing? I'm sorry if I am re-asking or combining many of the questions already on SO into a single question, but I just want to make perfect sense out of all the info that is out there on SO and the web regarding these concepts.

谢谢!

推荐答案

与您确定的差异相比,差异更大.

There are more differences than the ones you have identified.

双工/定向:

  • 单向:HTTP轮询,长轮询,流式传输.
  • 双向:WebSockets,插件网络

按照增加的延迟顺序(大约)进行操作:

In order of increasing latency (approximate):

  • WebSockets
  • 插件联网
  • HTTP流式传输
  • HTTP长轮询
  • HTTP轮询

CORS(跨域支持):

CORS (cross-origin support):

  • WebSockets:是
  • 插件联网:通过策略请求进行刷新(不确定其他人)
  • HTTP *(最近的一些支持)

本机二进制数据(类型化数组,blob):

Native binary data (typed arrays, blobs):

  • WebSockets:是
  • 插件联网:不支持Flash(需要通过ExternalInterface进行URL编码)
  • HTTP *:最近建议启用二进制类型支持

带宽在降低效率:

  • 插件网络:Flash套接字是原始策略请求请求之外的原始内容
  • WebSockets:连接设置握手和每帧几个字节
  • HTTP流式传输(重复使用服务器连接)
  • HTTP长轮询:每封邮件的连接
  • HTTP轮询:每条消息的连接+没有数据消息

移动设备支持:

  • WebSocket: iOS 4.2 and up. Some Android via Flash emulation or using Firefox for Android or Google Chrome for Android which both provide native WebSocket support.
  • Plugin networking: some Android. Not on iOS
  • HTTP *: mostly yes

JavaScript使用复杂性(从最简单到最复杂).可以肯定的是,复杂度度量是主观的.

Javascript usage complexity (from simplest to most complicated). Admittedly complexity measures are somewhat subjective.

  • WebSockets
  • HTTP民意调查
  • 插件联网
  • HTTP长轮询,流式传输

还要注意,有一个用于标准化HTTP流的W3C提案,称为服务器发送的事件 .目前,它尚处于发展初期,旨在提供一种标准的Javascript API,其简单性可与WebSockets相提并论.

Also note that there is a W3C proposal for standardizing HTTP streaming called Server-Sent Events. It is currently fairly early in it's evolution and is designed to provide a standard Javascript API with comparable simplicity to WebSockets.

这篇关于我对HTTP轮询,长轮询,HTTP流和WebSocket的理解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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