Supporting Multiple Versions of WebSocket Protocol 支持多版本WebSocket協議

https://tools.ietf.org/html/rfc6455#section-4.4html

4.4. Supporting Multiple Versions of WebSocket Protocol



   This section provides some guidance on supporting multiple versions
   of the WebSocket Protocol in clients and servers.

   Using the WebSocket version advertisement capability (the
   |Sec-WebSocket-Version| header field), a client can initially request
   the version of the WebSocket Protocol that it prefers (which doesn't
   necessarily have to be the latest supported by the client).  If the
   server supports the requested version and the handshake message is
   otherwise valid, the server will accept that version.  If the server
   doesn't support the requested version, it MUST respond with a
   |Sec-WebSocket-Version| header field (or multiple
   |Sec-WebSocket-Version| header fields) containing all versions it is
   willing to use.  At this point, if the client supports one of the
   advertised versions, it can repeat the WebSocket handshake using a
   new version value.

   The following example demonstrates version negotiation described
   above:

      GET /chat HTTP/1.1
      Host: server.example.com
      Upgrade: websocket
      Connection: Upgrade
      ...
      Sec-WebSocket-Version: 25






Fette & Melnikov Standards Track [Page 26]


RFC 6455                 The WebSocket Protocol            December 2011


   The response from the server might look as follows:

      HTTP/1.1 400 Bad Request
      ...
      Sec-WebSocket-Version: 13, 8, 7

   Note that the last response from the server might also look like:

      HTTP/1.1 400 Bad Request
      ...
      Sec-WebSocket-Version: 13
      Sec-WebSocket-Version: 8, 7

   The client now repeats the handshake that conforms to version 13:

      GET /chat HTTP/1.1
      Host: server.example.com
      Upgrade: websocket
      Connection: Upgrade
      ...
      Sec-WebSocket-Version: 13

 

 

使用WebSocket版本標記字段(`Sec-WebSocket-Version`header字段),客戶端能夠在最初請求時選擇WebSocket協議的版本號(客戶端沒必要要支持最新的版本)。若是服務端支持請求的版本而且我收到消息是有效的,那麼服務端會接受這個版本。若是服務端不支持客戶端請求的版本,那麼服務端必須返回一個`Sec-WebSocket-Version`header字段(或者多個`Sec-WebSocket-Version`header字段)包含服務端支持的全部版本。在這種狀況下,若是客戶端支持其中任意一個版本,它能夠選擇一個新的版本值從新發起握手請求。
相關文章
相關標籤/搜索