为了建立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
服务器响应:
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=
Sec-WebSocket-Protocol: chat
我无法理解。要初始化连接,将发送HTTP GET请求。但是如果我不主持HTTP服务器但我只托管WebSocket服务器怎么办?那么它如何处理HTTP请求呢?
答案 0 :(得分:7)
按照设计,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服务器,则无关紧要。 ANY 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请求,至少。实施起来并不难。
答案 1 :(得分:3)
所有webSocket连接都以带有标头的HTTP请求开始,该标头请求升级到webSocket协议。如果接收服务器同意,则双方将协议从HTTP切换到webSocket,然后连接使用webSocket协议。
因此,所有webSocket服务器必须支持该初始HTTP请求,因为这就是所有webSocket连接的启动方式。
webSocket协议是以这种方式设计的,原因有两个:
因此,单个主机和端口可用于常规HTTP和WebSocket连接。如果需要,可以使用单个服务器进程来处理这两种类型的连接。如果它们位于不同的端口上,则需要两个单独的服务器进程。
因此,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
请注意Upgrade: websocket
标题。这告诉接收HTTP服务器这是对webSocket连接的请求。如果服务器想要接受该连接,它将返回101响应,告知客户端他们现在可以切换协议:
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=
Sec-WebSocket-Protocol: chat
在此响应之后,客户端和服务器切换协议(在相同的TCP连接上)从那时起他们只会说webSocket协议并且TCP连接保持打开,直到客户端或服务器明确关闭它(通常是长期连接)
我无法理解。要初始化连接,将发送HTTP GET请求。但是,如果我不托管HTTP服务器但我只托管WebSocket服务器呢?
所有webSocket服务器都必须接受HTTP请求,因为所有webSocket连接都以HTTP请求开头。因此,没有webSocket服务器不接受HTTP请求。纯webSocket服务器只能接受具有Upgrade: webSocket
标头的HTTP请求,并且不会响应任何其他HTTP请求。
那么它如何处理HTTP请求呢?
所有webSocket服务器都希望传入的新连接以HTTP开头,因此它们必须内置一个简单的HTTP服务器(可以解析初始HTTP头)。