RFC6455-The WebSocket protocol 之七:6. Sending and Receiving Data

来源:互联网 发布:知乎帐号被停用怎么办 编辑:程序博客网 时间:2024/06/03 00:30

6.  Sending and Receiving Data

6、发送和接收数据


6.1.  Sending Data

6.1 发送数据
   To _Send a WebSocket Message_ comprising of /data/ over a WebSocket
   connection, an endpoint MUST perform the following steps.

在WebSocket连接上发送一个包含数据的WebSocket信息,终端必须执行以下的步骤。

   1.  The endpoint MUST ensure the WebSocket connection is in the

 OPEN
       state (cf. Sections 4.1 and 4.2.2.)  If at any point the state of
       the WebSocket connection changes, the endpoint MUST abort the
       following steps.

1、终端必须确保WebSocket连接处于OPEN状态,如果在任何时间WebSocket连接的状态发生了变化,终端必须终止后续操作。


   2.  An endpoint MUST encapsulate the /data/ in a WebSocket frame as
       defined in Section 5.2.  If the data to be sent is large or if
       the data is not available in its entirety at the point the
       endpoint wishes to begin sending the data, the endpoint MAY
       alternately encapsulate the data in a series of frames as defined
       in Section 5.4.

终端需要把数据封装到一个WebSocket数据包中。终端计划去发送数据的时候,如果发送的数据太大或作为一个整体有问题,终端可能会把数据封装到一系列的数据包中。


   3.  The opcode (frame-opcode) of the first frame containing the data
       MUST be set to the appropriate value from Section 5.2 for data
       that is to be interpreted by the recipient as text or binary
       data.

包含数据的第一个数据包的操作码必须设置合理,从而接收者可以对文本或二进制数据进行正确的解析。


   4.  The FIN bit (frame-fin) of the last frame containing the data
       MUST be set to 1 as defined in Section 5.2.

最后一个数据包的FIN位必须设置为1。


   5.  If the data is being sent by the client, the frame(s) MUST be
       masked as defined in Section 5.3.

假如数据是客户端发送的,数据包必须进行掩码。


   6.  If any extensions (Section 9) have been negotiated for the
       WebSocket connection, additional considerations may apply as per
       the definition of those extensions.

假如WebSocket连接定义了一些扩展,按照这些扩展的定义会增加一些额外的注意事项。


   7.  The frame(s) that have been formed MUST be transmitted over the
       underlying network connection.

被加工的数据包必须在网络连接上进行传输。


6.2.  Receiving Data

6.2 接收数据
   To receive WebSocket data, an endpoint listens on the underlying
   network connection.  Incoming data MUST be parsed as WebSocket frames
   as defined in Section 5.2.  If a control frame (Section 5.5) is
   received, the frame MUST be handled as defined by Section 5.5.  Upon
   receiving a data frame (Section 5.6), the endpoint MUST note the
   /type/ of the data as defined by the opcode (frame-opcode) from
   Section 5.2.  The "Application data" from this frame is defined as
   the /data/ of the message.  If the frame comprises an unfragmented
   message (Section 5.4), it is said that _A WebSocket Message Has Been
   Received_ with type /type/ and data /data/.  If the frame is part of
   a fragmented message, the "Application data" of the subsequent data
   frames is concatenated to form the /data/.  When the last fragment is
   received as indicated by the FIN bit (frame-fin), it is said that _A
   WebSocket Message Has Been Received_ with data /data/ (comprised of
   the concatenation of the "Application data" of the fragments) and   type /type/ (noted from the first frame of the fragmented message).
   Subsequent data frames MUST be interpreted as belonging to a new
   WebSocket message.
   Extensions (Section 9) MAY change the semantics of how data is read,
   specifically including what comprises a message boundary.
   Extensions, in addition to adding "Extension data" before the
   "Application data" in a payload, MAY also modify the "Application
   data" (such as by compressing it).
   A server MUST remove masking for data frames received from a client
   as described in Section 5.3.

为了接收数据,终端需要监听网络连接。传入的数据必须转换成WebSocket数据包的格式。假如接收到了控制数据包,则按照5.5节所述处理。一旦接收到一个数据包,终端需要注意到操作码的内容。数据包中的应用数据是消息的内容。假如数据包是没有分片的信息,接收方会类型、数据一块接收。假如数据包是分片数据的一个,后续的数据包的应用数据组成了完整的数据。假如以FIN位标记的最后一个分片被接收了,就意味着WebSocket消息的数据(各分片中应用数据的和)、类型(分片信息中的第一个分片中)都被接收了。后续的数据包必须被看成是一个新的WebSocket消息。

扩展可能会改变数据如何去读的规则,尤其是包含消息边界的时候。扩展信息,除了会在负载的应用数据之前增加扩展数据之外,还可能修改应用数据(例如压缩它)。服务端必须对从客户端接收到的数据进行解掩码操作。

0 0
原创粉丝点击