Bosh vs Comet vs Long Polling vs Polling

最近在研究在中国特色移动互联网下通过性最好的应用层协议办法,看到很多混乱的概念和实现,54chen在对各种名词的历史进行了深度考察,特作记录以做区分。

HTTP协议历史

  • 第一个HTTP协议诞生于1989年3月。
  • 第一个HTTP协议的版本是HTTP 0.9,它的组成极其简单,因为它只允许客户端发送GET这一种请求。
  • HTTP协议的第二个版本是HTTP 1.0,直到HTTP 1.0成为最重要的面向事务的应用层协议。该协议对每一次请求/响应,同样是建立并关闭一次连接。
  • HTTP协议的第三个版本是HTTP 1.1,它就是目前使用最广泛的协议版本。从HTTP 1.1开始,客户端默认与Web 服务器建立长连接。

原始阶段polling

最早期的时候,因为没有可以依赖的东西,能用的只有javascript的setInterval,定时循环向服务器发起请求。

优点是:服务器端啥特别的东西都不用准备,直接当http请求处理即可。缺点是:白白浪费大量的请求。

什么是Comet?

中文意思彗星,大概是想表示这是一次尾巴会拖很长的请求。

Comet和ajax一样,都是一堆技术的合体叫做Comet(莫非是javascript界很喜欢搞高大上的名词?)。

初始阶段的Comet

  • CometP属于这种(jsonP+iframe)。
  • Frame Comet也属于这种(json+iframe)。
  • ajax Comet也属于这种(ajax)
  • HTTP1.1是长连接,而且服务器chunked encoding机制可以让结果部分输出。
  • 利用HTTP1.1的特性,再配合iframe src到一个url,一旦有数据,通过javascript的top.func()把数据告诉浏览器。
  • 优点是:比起polling节省了更多的带宽。缺点:用户打开多个浏览器时会占用多条连接,所以必须在server端配合使用非阻塞的http server,同时如果网络不太稳定,容易断掉。

改良版的Comet:long polling

  • 因为发现网络不稳定容易断的问题。
  • 改良后,添加了每次请求返回后立即重新发起请求机会--长轮循。
  • 优点:比起初始阶段,不容易因网络断了连接,是当今最常见的解决方案。缺点:实在找不到啥缺点,唯一的缺点就是利用了http协议,要是tcp层的,直接连接上收发数据就行了。

双向可通的WebSocket

  • 前面都是说的http的,html5就来了个WebSocket,WEB也可以进行TCP连接了。
  • 各浏览器实现略有不同。
  • 优点:底层连接 缺点:好多浏览器不支持。

BOSH(Bidirectional-streams Over Synchronous HTTP)

  • 这是xmpp协议里定义的一个扩展协议,实际上也可以归结为Comet技术的合体。
  • 它同时支持老的polling也支持long polling。
  • 具体实现中要完成交互协议才行。
  • 优点:若实现细节可自动适应http 1.0 1.1  缺点:实现要更复杂。

结尾

  • 各种让人混淆的名词,其实都是在做同一件事情。
  • http streaming = http chunked encoding = http keepalive = http 1.1
时间: 2024-08-02 15:29:41

Bosh vs Comet vs Long Polling vs Polling的相关文章

HTTP Polling, Long Polling, HTTP Streaming and WebSockets

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 ma

Microsoft.AspNet.SignalR 2.2

Nuget :http://www.nuget.org/packages/Microsoft.AspNet.SignalR/ What is SignalR? ASP.NET SignalR is a library for ASP.NET developers that simplifies the process of adding real-time web functionality to applications. Real-time web functionality is the

Bayeux协议

Bayeux 协议-- Bayeux 1.0草案1 本备忘录状态 This document specifies a protocol for the Internet community, and requests discussion and suggestions for improvement. This memo is written in the style and spirit of an IETF RFC but is not, as of yet, an official IE

【DWR系列02】-DWR逆向Ajax即服务器推送

.literal { background-color: #f2f2f2; border: 1px solid #cccccc; padding: 1px 3px 0; white-space: nowrap; color: #6d180b; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 16px } 一.简单例子直观认识 1.1 模拟场景 假定项目中需要新增一个功能,管理员发布某些信息

转:实现一个简单的服务端推送方案

原文来自于:http://blog.csdn.net/jiao_fuyou/article/details/17090355 客户端和服务端的交互有推和拉两种方式:如果是客户端拉的话,通常就是Polling:如果是服务端推的话,一般就是Comet,目前比较流行的Comet实现方式是Long Polling. 注:如果不清楚相关名词含义,可以参考:Browser 與 Server 持續同步的作法介紹. 先来看看Polling,它其实就是我们平常所说的轮询,大致如下所示: Polling 因为服务端

服务器推送(Server push)技术总结

1. 短轮询 ajax按一定间隔去请求 2. 长轮询(long Polling) Long Polling的实现很简单,可分为四个过程: 发起Polling发起Polling很简单,只需向服务器发起请求,此时服务端还未应答,所以客户端与服务端之间一直处于连接状态. 数据推送如果服务器端有相关数据,此时服务端会将数据通过此前建立的通道发回客户端. Polling终止 Polling终止情况有三种:若服务端返回相关数据,此时客户端收到数据后,关闭请求连接,结束此次Polling过程.若客户端等待设定

Safe

GC safe-point (or safepoint) and safe-region Root referencesAn object is dead really means it is useless. Only the programmer knows if an object is useless or not. In order for the program to decide if an object is useless, we can use compiler analys

C# 内存模型

C# 内存模型 This is the first of a two-part series that will tell the long story of the C# memory model. The first part explains the guarantees the C# memory model makes and shows the code patterns that motivate the guarantees; the second part will detai

springmvc(18)使用WebSocket 和 STOMP 实现消息功能

[0]README 1)本文旨在 介绍如何 利用 WebSocket 和 STOMP 实现消息功能: 2)要知道, WebSocket 是发送和接收消息的 底层API,而SockJS 是在 WebSocket 之上的 API:最后 STOMP(面向消息的简单文本协议)是基于 SockJS 的高级API (干货--简而言之,WebSocket 是底层协议,SockJS 是WebSocket 的备选方案,也是 底层协议,而 STOMP 是基于 WebSocket(SockJS) 的上层协议) 3)b