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








分类: 系统运维

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.



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:


GET ws:// HTTP/1.1


Cookie: __utma=99as

Connection: Upgrade



Upgrade: websocket Sec-WebSocket-Version: 13


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


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-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.



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.




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.



阅读(2635) | 评论(0) | 转发(0) |