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

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消息。

擴展可能會改變數據如何去讀的規則,尤其是包含消息邊界的時候。擴展信息,除了會在負載的應用數據之前增加擴展數據之外,還可能修改應用數據(例如壓縮它)。服務端必須對從客戶端接收到的數據進行解掩碼操作。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章