Supporting Multiple Versions of WebSocket Protocol 支持多版本WebSocket协议

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

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字段)包含服务端支持的所有版本。在这种情况下,如果客户端支持其中任意一个版本,它可以选择一个新的版本值重新发起握手请求。
 
 
posted @ 2020-05-26 12:55  papering  阅读(225)  评论(0编辑  收藏  举报