websocket实现内部使用http协议吗?

Kon*_*rad 3 sockets websocket

为了建立WebSocket连接,客户端发送WebSocket握手请求,服务器为此返回WebSocket握手响应,如下例所示.[30]

客户端请求(就像在HTTP中一样,每行以\ r \n结尾,并且末尾必须有一个额外的空行):

GET /chat HTTP/1.1
Host: server.example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==
Sec-WebSocket-Protocol: chat, superchat
Sec-WebSocket-Version: 13
Origin: http://example.com
Run Code Online (Sandbox Code Playgroud)

服务器响应:

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=
Sec-WebSocket-Protocol: chat
Run Code Online (Sandbox Code Playgroud)

我无法理解.要初始化连接,将发送HTTP GET请求.但是,如果我不托管HTTP服务器但我只托管WebSocket服务器怎么办?那么它如何处理HTTP请求呢?

Rem*_*eau 8

根据设计,WebSocket协议握手使用HTTP,因此可以在现有HTTP服务器和其他基于HTTP的技术中使用WebSockets:

   The WebSocket Protocol is designed to supersede existing
   bidirectional communication technologies that use HTTP as a transport
   layer to benefit from existing infrastructure (proxies, filtering,
   authentication).  Such technologies were implemented as trade-offs
   between efficiency and reliability because HTTP was not initially
   meant to be used for bidirectional communication (see [RFC6202] for
   further discussion).  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.  This last
   point is important because the traffic patterns of interactive
   messaging do not closely match standard HTTP traffic and can induce
   unusual loads on some components.

但是,一旦WebSocket握手完成,只使用WebSocket协议,而不再使用HTTP.

因此,如果您使用具有WebSocket支持的HTTP服务器或专用WebSocket服务器,则无关紧要.任何 WebSocket实现必须使用HTTP(以及其中的所有语义,包括身份验证,重定向等)进行初始握手.它由WebSocket协议规范RFC 6455强制要求.

   The opening handshake is intended to be compatible with HTTP-based
   server-side software and intermediaries, so that a single port can be
   used by both HTTP clients talking to that server and WebSocket
   clients talking to that server.  To this end, the WebSocket client's
   handshake is an HTTP Upgrade request

因此,专用WebSockets服务器必须能够在握手阶段处理HTTP请求,至少.实施起来并不难.


jfr*_*d00 6

websocket 实现是否在内部使用 http 协议?

是的,最初,然后他们切换到 webSocket 协议。所有 webSocket 连接都以一个 HTTP 请求开始,该请求带有一个请求升级到 webSocket 协议的标头。如果接收服务器同意,那么双方将协议从 HTTP 切换到 webSocket,然后连接使用 webSocket 协议。

因此,所有 webSocket 服务器都必须支持该初始 HTTP 请求,因为这就是所有 webSocket 连接的启动方式。

webSocket 协议是这样设计的,有以下几个原因:

  1. 因此,单个主机和端口可用于常规 HTTP 和 webSocket 连接。如果需要,您可以使用单个服务器进程来处理这两种类型的连接。如果它们在不同的端口上,您将需要两个单独的服务器进程。

  2. 因此,webSockets 可以在端口 80 或 443(连同 HTTP 服务器)上运行,以最大程度地与各种已部署的网络基础设施(例如公司代理、防火墙等)兼容。这些基础设施可能只允许常规 HTTP 端口上的流量。

正如您所见,一个 webSocket 请求以这样的 HTTP 请求开始:

GET /chat HTTP/1.1
Host: server.example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==
Sec-WebSocket-Protocol: chat, superchat
Sec-WebSocket-Version: 13
Origin: http://example.com
Run Code Online (Sandbox Code Playgroud)

注意Upgrade: websocket标题。这告诉接收方的 HTTP 服务器这是对 webSocket 连接的请求。如果服务器想要接受该连接,它会返回一个 101 响应,告诉客户端他们现在可以切换协议:

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=
Sec-WebSocket-Protocol: chat
Run Code Online (Sandbox Code Playgroud)

在这个响应之后,客户端和服务器都切换协议(在同一个 TCP 连接上),从那时起它们只说 webSocket 协议并且 TCP 连接保持打开状态,直到客户端或服务器明确关闭它(通常是一个长期存在的连接)。

我无法理解。发送 HTTP GET 请求以初始化连接。但是如果我不托管 HTTP 服务器,而只托管 WebSocket 服务器呢?

所有 webSocket 服务器都必须接受 HTTP 请求,因为所有 webSocket 连接都以 HTTP 请求开始。因此,不存在不接受 HTTP 请求的 webSocket 服务器。纯 webSocket only 服务器只能接受具有Upgrade: webSocket标头的HTTP 请求,而其他任何 HTTP 请求都失败。

那么它如何处理 HTTP 请求呢?

所有 webSocket 服务器都希望传入的新连接以 HTTP 开头,因此它们必须内置一个简单的 HTTP 服务器(可以解析初始 HTTP 标头)。