WebSocket的设计是为了让其服务器可以与HTTP服务器共享端口,通过进行有效的HTTP升级请求来完成握手。
我对此设计哲学有疑问。无论如何,WebSocket协议都是一种独立的基于TCP的协议。
为什么我们需要这个HTTP握手(升级请求)和协议切换呢?难道不能直接(并独立地)遵循类似于WebSocket的协议吗?
WebSocket的设计是为了让其服务器可以与HTTP服务器共享端口,通过进行有效的HTTP升级请求来完成握手。
我对此设计哲学有疑问。无论如何,WebSocket协议都是一种独立的基于TCP的协议。
为什么我们需要这个HTTP握手(升级请求)和协议切换呢?难道不能直接(并独立地)遵循类似于WebSocket的协议吗?
The WebSocket Protocol attempts to address the goals of existing
bidirectional HTTP technologies in the context of the existing HTTP
infrastructure; as such, it is designed to work over HTTP ports 80
and 443 as well as to support HTTP proxies and intermediaries, even
if this implies some complexity specific to the current environment.
However, the design does not limit WebSocket to HTTP, and future
implementations could use a simpler handshake over a dedicated port
without reinventing the entire protocol.
换句话说,已经存在着大量的 HTTP 和 HTTPS 基础设施(代理、防火墙、缓存和其他中介),为了增加广泛采用的可能性,WebSocket 协议被设计为允许对现有基础设施进行调整和扩展,而无需完全重新创建来支持在专用端口上使用新协议。
值得注意的是,即使 WebSocket 协议摆脱了与 HTTP 兼容的握手,它仍需要几乎等价复杂度的握手来支持现代 Web 的安全要求,这样浏览器和服务器才能相互验证并支持安全的跨源请求共享 (CORS)。甚至“原始”的 Flash socket 在创建实际 socket 之前也会通过安全策略请求与服务器进行握手。