websocket 实现是否在内部使用 http 协议? [英] Do websocket implementations use http protocol internally?

查看:57
本文介绍了websocket 实现是否在内部使用 http 协议?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

To establish a WebSocket connection, the client sends a WebSocket handshake request, for which the server returns a WebSocket handshake response, as shown in the example below.[30]

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

Client request (just like in HTTP, each line ends with \r\n and there must be an extra blank line at the end):

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 请求?

I can't understand it. To initialize the connection HTTP GET request is sent. But what if I don't host HTTP server but I just host WebSocket server? How does it handle HTTP requests then?

推荐答案

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

By design, the WebSocket protocol handshake uses HTTP so WebSockets can be utilized in existing HTTP servers and other HTTP-based technologies:

WebSocket 协议旨在取代使用 HTTP 作为传输层的现有双向通信技术,以从现有基础设施(代理、过滤、身份验证)中受益.此类技术被实现为效率和可靠性之间的权衡,因为 HTTP 最初并不打算用于双向通信(请参阅 [RFC6202] 进一步讨论).WebSocket 协议试图在现有 HTTP 基础设施的上下文中解决现有双向 HTTP 技术的目标;因此,它旨在通过 HTTP 端口 80 和 443 工作并支持 HTTP 代理和中介,即使这意味着某些特定于当前环境的复杂性.但是,该设计并未将 WebSocket 限制为 HTTP,未来的实现可以在专用端口上使用更简单的握手,而无需重新发明整个协议.最后一点很重要,因为交互式消息传递的流量模式与标准 HTTP 流量并不紧密匹配,并且可能会导致某些组件出现异常负载.

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.

But, once the WebSocket handshake is finished, only the WebSocket protocol is used, not HTTP anymore.

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

So, it doesn't matter if you use an HTTP server with WebSocket support, or a dedicated WebSocket server. ANY WebSocket implementation MUST use HTTP (and all semantics therein, including authentication, redirection, etc) for the initial handshake. It is mandated by the WebSocket protocol specification, RFC 6455.

打开握手旨在与基于 HTTP 的服务器端软件和中介兼容,以便与该服务器通信的 HTTP 客户端和与该服务器通信的 WebSocket 客户端都可以使用单个端口.为此,WebSocket 客户端的握手是一个 HTTP Upgrade 请求

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 请求.实施起来并不难.

So, a dedicated WebSockets server must be able to handle HTTP requests during the handshake phase, at least. It is not that hard to implement.

这篇关于websocket 实现是否在内部使用 http 协议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆