1)为什么WebSockets协议更好?
WebSockets更适合于涉及低延迟通信的情况,特别是客户端到服务器消息的低延迟。对于服务器到客户端数据,使用长连接和分块传输可以获得相当低的延迟。然而,这并不能解决客户端到服务器的延迟问题,因为这需要为每个客户端到服务器的消息建立一个新的连接。
你的48字节HTTP握手对于现实世界的HTTP浏览器连接是不现实的,在现实世界中,经常有几千字节的数据作为请求的一部分(在两个方向上)发送,包括许多报头和cookie数据。下面是一个使用Chrome的请求/响应的例子:
请求示例(包含cookie数据的2800字节,不包含cookie数据的490字节):
GET / HTTP/1.1
Host: www.cnn.com
Connection: keep-alive
Cache-Control: no-cache
Pragma: no-cache
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.68 Safari/537.17
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: [[[2428 byte of cookie data]]]
示例响应(355字节):
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 13 Feb 2013 18:56:27 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
Set-Cookie: CG=US:TX:Arlington; path=/
Last-Modified: Wed, 13 Feb 2013 18:55:22 GMT
Vary: Accept-Encoding
Cache-Control: max-age=60, private
Expires: Wed, 13 Feb 2013 18:56:54 GMT
Content-Encoding: gzip
HTTP和WebSockets都有相同大小的初始连接握手,但是对于WebSocket连接,初始握手执行一次,然后小消息只有6个字节的开销(头部2个字节,掩码值4个字节)。延迟开销与头文件的大小无关,而是与解析/处理/存储这些头文件的逻辑有关。此外,TCP连接建立延迟可能是比每个请求的大小或处理时间更大的因素。
2)为什么实现它而不是更新HTTP协议?
有努力重新设计HTTP协议,以实现更好的性能和更低的延迟,如SPDY, HTTP 2.0和QUIC。这将改善正常HTTP请求的情况,但WebSockets和/或WebRTC DataChannel的客户端到服务器数据传输的延迟可能仍然比HTTP协议低(或者它将以一种看起来很像WebSockets的模式使用)。
更新:
下面是一个考虑web协议的框架:
TCP: low-level, bi-directional, full-duplex, and guaranteed order transport layer. No browser support (except via plugin/Flash).
HTTP 1.0: request-response transport protocol layered on TCP. The client makes one full request, the server gives one full response, and then the connection is closed. The request methods (GET, POST, HEAD) have specific transactional meaning for resources on the server.
HTTP 1.1: maintains the request-response nature of HTTP 1.0, but allows the connection to stay open for multiple full requests/full responses (one response per request). Still has full headers in the request and response but the connection is re-used and not closed. HTTP 1.1 also added some additional request methods (OPTIONS, PUT, DELETE, TRACE, CONNECT) which also have specific transactional meanings. However, as noted in the introduction to the HTTP 2.0 draft proposal, HTTP 1.1 pipelining is not widely deployed so this greatly limits the utility of HTTP 1.1 to solve latency between browsers and servers.
Long-poll: sort of a "hack" to HTTP (either 1.0 or 1.1) where the server does not respond immediately (or only responds partially with headers) to the client request. After a server response, the client immediately sends a new request (using the same connection if over HTTP 1.1).
HTTP streaming: a variety of techniques (multipart/chunked response) that allow the server to send more than one response to a single client request. The W3C is standardizing this as Server-Sent Events using a text/event-stream MIME type. The browser API (which is fairly similar to the WebSocket API) is called the EventSource API.
Comet/server push: this is an umbrella term that includes both long-poll and HTTP streaming. Comet libraries usually support multiple techniques to try and maximize cross-browser and cross-server support.
WebSockets: a transport layer built-on TCP that uses an HTTP friendly Upgrade handshake. Unlike TCP, which is a streaming transport, WebSockets is a message based transport: messages are delimited on the wire and are re-assembled in-full before delivery to the application. WebSocket connections are bi-directional, full-duplex and long-lived. After the initial handshake request/response, there is no transactional semantics and there is very little per message overhead. The client and server may send messages at any time and must handle message receipt asynchronously.
SPDY: a Google initiated proposal to extend HTTP using a more efficient wire protocol but maintaining all HTTP semantics (request/response, cookies, encoding). SPDY introduces a new framing format (with length-prefixed frames) and specifies a way to layering HTTP request/response pairs onto the new framing layer. Headers can be compressed and new headers can be sent after the connection has been established. There are real world implementations of SPDY in browsers and servers.
HTTP 2.0: has similar goals to SPDY: reduce HTTP latency and overhead while preserving HTTP semantics. The current draft is derived from SPDY and defines an upgrade handshake and data framing that is very similar the the WebSocket standard for handshake and framing. An alternate HTTP 2.0 draft proposal (httpbis-speed-mobility) actually uses WebSockets for the transport layer and adds the SPDY multiplexing and HTTP mapping as an WebSocket extension (WebSocket extensions are negotiated during the handshake).
WebRTC/CU-WebRTC: proposals to allow peer-to-peer connectivity between browsers. This may enable lower average and maximum latency communication because as the underlying transport is SDP/datagram rather than TCP. This allows out-of-order delivery of packets/messages which avoids the TCP issue of latency spikes caused by dropped packets which delay delivery of all subsequent packets (to guarantee in-order delivery).
QUIC: is an experimental protocol aimed at reducing web latency over that of TCP. On the surface, QUIC is very similar to TCP+TLS+SPDY implemented on UDP. QUIC provides multiplexing and flow control equivalent to HTTP/2, security equivalent to TLS, and connection semantics, reliability, and congestion control equivalentto TCP. Because TCP is implemented in operating system kernels, and middlebox firmware, making significant changes to TCP is next to impossible. However, since QUIC is built on top of UDP, it suffers from no such limitations. QUIC is designed and optimised for HTTP/2 semantics.
引用:
HTTP:
维基百科HTTP页面
HTTP相关草案/协议的W3C列表
IETF HTTP/1.1和HTTP/2.0草案列表
服务器发送的事件:
W3C服务器发送事件/事件源候选推荐
W3C服务器发送事件/事件源草案
尚:
IETF RFC 6455 WebSockets协议
IETF RFC 6455 WebSocket勘误表
SPDY:
IETF SPDY草案
HTTP 2.0:
IETF HTTP 2.0 httpbis-http2草案
IETF HTTP 2.0 httpbis-speed-mobility草案
IETF httpbis网络友好草案——一个较旧的HTTP 2.0相关提案
WebRTC:
W3C WebRTC API草案
IETF WebRTC草案列表
IETF WebRTC概述草案
IETF WebRTC数据通道草案
微软CU-WebRTC提案开始页
QUIC:
QUIC铬项目
IETF QUIC草案