Do websocket implementations use http protocol internally?

后端 未结 2 1597
臣服心动
臣服心动 2020-12-16 06:42

To establish a WebSocket connection, the client sends a WebSocket handshake request, for which the server returns a WebSocket handshake response, as shown in th

2条回答
  •  孤街浪徒
    2020-12-16 07:24

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

       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.
    

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

    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.

       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
    

    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.

提交回复
热议问题