Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3818728
  • 博文数量: 291
  • 博客积分: 8003
  • 博客等级: 大校
  • 技术积分: 4275
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-30 18:28
文章分类

全部博文(291)

文章存档

2017年(1)

2013年(47)

2012年(115)

2011年(121)

2010年(7)

分类: 系统运维

2012-08-05 11:55:58

关于HTML5 WebSockets

The HTML5 WebSockets specification defines an API that enables web pages to use the WebSockets protocol for two-way communication with a remote host. It introduces the WebSocket interface and defines a full-duplex communication channel that operates through a single socket over the Web. HTML5 WebSockets provide an enormous reduction in unnecessary network traffic and latency compared to the unscalable polling and long-polling solutions that were used to simulate a full-duplex connection by maintaining two connections.

HTML WebSockets 规范定义了一个API使网页能够使用WebSockets协议和远程主机进行双向通讯。HTML WebSockets 规范介绍WebSocket接口并且定义了一个全双工的信道,这个信道是通过一个单独的跨越网络的socket连接来进行工作的。HTML5 WebSockets大量降低了不必要的网络流量,并且相比用于通过该维护两个连接来模拟全双工连接的、不能扩展的轮询和长轮询方案来说, HTML5 WebSockets是一个潜在竞争对手。

 

HTML5 WebSockets account for network hazards such as proxies and firewalls, making streaming possible over any connection, and with the ability to support upstream and downstream communications over a single connection, HTML5 WebSockets-based applications place less burden on servers, allowing existing machines to support more concurrent connections. The following figure shows a basic WebSocket-based architecture in which browsers use a WebSocket connection for full-duplex, direct communication with remote hosts.

HTML5 WebSockets 在各种网络障碍例如代理和防火墙下,是流量能够通过任意连接,并且有能力只靠单个连接支持上行流量和下行流量,基于HTML5 WebSockets的应用能够减轻服务器负担,让目前的机器支持更多并发连接。下面的特性表现了在一个基本的基于WebSocket的架构中,浏览器使用WebSocket 连接和远程主机进行全双工,直接通讯。

 

One of the more unique features WebSockets provide is its ability to traverse firewalls and proxies, a problem area for many applications. -style applications typically employ long-polling as a rudimentary line of defense against firewalls and proxies. The technique is effective, but is not well suited for applications that have sub-500 millisecond latency or high throughput requirements. Plugin-based technologies such as Adobe Flash, also provide some level of socket support, but have long been burdened with the very proxy and firewall traversal problems that WebSockets now resolve.

WebSockets提供的众多特性之一是它能够穿透防火墙和代理,解决了很多软件的一个问题领域。Comet类型的软件采用长连接作为基本的防止防火墙和代理的防线。这种技术是有效的,但是不太适合那些有500毫秒以内或高吞吐量的需求的应用。插件式的技术例如AdobeFlash,也可以提供一定程度的支持,但是也是有因为代理和防火墙穿透成为服务器负担的问题,而这个问题WebSocket现在已经解决。

 

A WebSocket detects the presence of a proxy server and automatically sets up a tunnel to pass through the proxy. The tunnel is established by issuing an HTTP CONNECT statement to the proxy server, which requests for the proxy server to open a TCP/IP connection to a specific host and port. Once the tunnel is set up, communication can flow unimpeded through the proxy. Since HTTP/S works in a similar fashion, secure WebSockets over SSL can leverage the same HTTP CONNECT technique. Note that WebSockets are just beginning to be supported by modern browsers (Chrome now supports WebSockets natively). However, backward-compatible implementations that enable today's browsers to take advantage of this emerging technology are available.

一个WebSocket请求探测一个存在的代理服务器,并且自动设置一个通道穿过代理服务器。这个通道被建立是通过分配一个HTTP 连接句柄连接到代理服务器,对于代理服务器需要做的是针对某个主机和端口打开一个TCP/IP连接。一旦通道设置好,通信能够畅通无阻通过代理服务器。因为HTTPSHTTP工作原理相似,在SSL上的安全WebSockets 能够在相同的HTTP连接技术上起作用。必须注意WebSockets只是开始被现代浏览器开始支持(Chrome一开始就支持WebSockets)。虽然如此,向后兼容性的实现使得当今的浏览器利用新兴技术成为可能。

WebSockets—like other pieces of the HTML5 effort such as Local Storage and Geolocation—was originally part of the HTML5 specification, but was moved to a separate standards document to keep the specification focused. WebSockets has been submitted to the Internet Engineering Task Force (IETF) by its creators, the (WHATWG). Authors, evangelists, and companies involved in the standardization still refer to the original set of features, including WebSockets, as "HTML5."

WebSockets-就像HTML5成就中的其他部分(例如本地存储和地理位置)--HTML5规范的原始定义的一部分,但是为了让规范更加瞩目,而被单独标准文档专门介绍。WebSockets 已经被作者(超文本应用技术工作组,WHATWG)提交到互联网工程任务组(IETF),各个作者,传播者和公司参与制定一系列原始的特点,也包括WebSockets的标准,这个标准就是HTML5

The WebSocket Protocol

WebSocket 协议

The WebSocket protocol was designed to work well with the existing Web infrastructure. As part of this design principle, the protocol specification defines that the WebSocket connection starts its life as an HTTP connection, guaranteeing full backwards compatibility with the pre-WebSocket world. The protocol switch from HTTP to WebSocket is referred to as a the WebSocket handshake.

WebSocket 协议设计成能够很好工作在现有的网络基础设施。作为设计原理的一部分,协议规格定义了WebSocket 连接它的生命周期从一个HTTP连接开始,和之前的WebSocket保持很好的向后兼容性。这个协议实现从HTTP协议转到WebSocket协议,因此被成为WebSocket桥梁。

The browser sends a request to the server, indicating that it wants to switch protocols from HTTP to WebSocket. The client expresses its desire through the Upgrade header:

浏览器发送一个请求到服务器,表示它想把HTTP协议转为WebSocket系统。客户端通过修改HTTP头部实现了这个需要,在头部里增加“Upgrade字段”。下面是发送的HTTP头部

GET ws://echo.websocket.org/?encoding=text HTTP/1.1

Origin:

Cookie: __utma=99as

Connection: Upgrade

Host: echo.websocket.org

Sec-WebSocket-Key:

Upgrade: websocket Sec-WebSocket-Version: 13

 

If the server understands the WebSocket protocol, it agrees to the protocol switch through the Upgrade header.

如果服务器识别WebSocket协议,它通过被修改的HTTP头部中的“Upgrade”字段接受协议转换。

HTTP/1.1 101 WebSocket Protocol Handshake

Date: Fri, 10 Feb 2012 17:38:18 GMT

Connection: Upgrade

Server: Kaazing Gateway

Upgrade: WebSocket

Access-Control-Allow-Origin:

Access-Control-Allow-Credentials: true

Sec-WebSocket-Accept: rLHCkw/SKsO9GAH/ZSFhBATDKrU=

Access-Control-Allow-Headers: content-type

 

At this point the HTTP connection breaks down and is replaced by the WebSocket connection over the same underlying TCP/IP connection. The WebSocket connection uses the same ports as HTTP (80) and HTTPS (443), by default.

在这点上HTTP连接会分解,取而代之的是同一基于TCP/IP连接的WebSocket连接。WebSockets连接默认使用和HTTP(80)或者HTTPS(443)一样的端口。

 

At this point the HTTP connection breaks down and is replaced by the WebSocket connection over the same underlying TCP/IP connection. The WebSocket connection uses the same ports as HTTP (80) and HTTPS (443), by default.

 

在这点上HTTP连接会分解,取而代之的是同一基于TCP/IP连接的WebSocket连接。WebSockets连接默认使用和HTTP(80)或者HTTPS(443)一样的端口。

 

Once established, WebSocket data frames can be sent back and forth between the client and the server in full-duplex mode. Both text and binary frames can be sent in either direction at the same time. The data is minimally framed with just two bytes. In the case of text frames, each frame starts with a 0x00 byte, ends with a 0xFF byte, and contains UTF-8 data in between. WebSocket text frames use a terminator, while binary frames use a length prefix.

一旦连接建立,WebSocket数据帧能够在客户端以全双工的模式来回发送。物理是文本还是二进制帧任何一个都可以在同一时间直接发送。最小的帧的数据量仅为2字节。若是文本帧的话,每一帧都是以0x00字节开头,以0xFF字节结尾,中间是UTF-8数据。WebSocket文本帧使用分隔符,而二进制帧使用的是把长度放在前端的方式(见下图利用两个字节来装操作码和长度)

 

阅读(2635) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~