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

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

问题描述

我在 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 Streaming: 类似于长轮询,但服务器使用带有传输编码:分块"的标头进行响应,因此我们不需要在服务器每次发送一些数据时发起新请求(从而节省额外的标头开销).这里的缺点是我们必须理解"并弄清楚数据的结构才能区分服务器发送的多个块.

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:

  • WebSockets 与 Java Applets、Flash 或 Silverlight 等插件相比的唯一优势是 WebSockets 原生内置于浏览器中,不依赖于插件.
  • WebSockets 相对于 http 流的唯一优势是您无需努力理解"和解析接收到的数据.
  • WebSockets 与长轮询相比的唯一优势是消除了额外的标头大小 &为请求打开和关闭套接字连接.

我还遗漏了其他任何显着差异吗?如果我重新询问或将 SO 上已有的许多问题合并为一个问题,我很抱歉,但我只想从 SO 和网络上有关这些概念的所有信息中完全理解.

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:是的
  • 插件网络:通过政策请求 Flash(不确定其他人)
  • 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 长轮询,流式传输

另请注意,W3C 有一项标准化 HTTP 流的提案,称为 Server-Sent Events.它目前处于发展的早期阶段,旨在提供一个标准的 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 流和 WebSockets 的理解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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