【问题标题】:Why websocket needs an opening handshake using HTTP? Why can't it be an independent protocol?为什么 websocket 需要使用 HTTP 进行打开握手?为什么不能是独立的协议?
【发布时间】:2013-11-03 07:23:31
【问题描述】:

Websocket 的设计使其服务器可以与 HTTP 服务器共享一个端口,方法是让其握手成为有效的 HTTP 升级请求。

我对这种设计理念持怀疑态度。 无论如何,WebSocket 协议都是一个独立的基于 TCP 的协议。

为什么我们需要这个 HTTP 握手(升级请求)和协议切换。相反,为什么我们不能直接(独立地)遵循类似 websocket 的协议?

【问题讨论】:

  • 嗨,我已经读过这篇文章,但这有点像在一堆干草中找到一根针。所以我会在隐藏答案的地方放上确切的句子“在相对简单的设置中,只有一个 IP 地址和一个服务器来处理到单个主机名的所有流量,这(HTTP 握手)可能为基于 WebSocket 协议的系统提供了一种实用的方法被部署。”参考:tools.ietf.org/html/rfc6455#section-1.8

标签: http websocket protocols


【解决方案1】:

引用IETF 6455 WebSocket spec

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 套接字也会在创建实际套接字之前通过安全策略请求与服务器握手。

【讨论】:

  • 试图理解协议。你的回答真的很有帮助。我想使用一个新的端口和应用程序来处理 WebSocket。有了你的回答,我想我也可以使用 apache 来处理 WebSocket 协议。但是是否有任何现有的 apache 模块可以处理 WS 数据帧?您有任何建议或链接可能有助于实现这一点吗?
  • 一些 websocket 扩展期望/假设 websocket 是通过 http 路径(无论是 HTTP/1.1、SPDY 还是 HTTP/2)作为升级建立的。例如,mux 扩展具有 addChannel 逻辑,期望 HTTP 请求正常运行。
猜你喜欢
  • 1970-01-01
  • 2015-12-26
  • 1970-01-01
  • 2013-02-13
  • 2021-02-05
  • 1970-01-01
  • 1970-01-01
  • 2018-02-23
  • 2017-01-04
相关资源
最近更新 更多