How does the webSocket server determine that the client receives the message sent by the server, that is, the reliability of the message:
The solution is to enable the server and client to send Ping/Pong Frame (
RFC 6455 - The WebSocket Protocol
). This kind of Frame is a special kind of data packet, which only contains some metadata and does not require real Data Payload, which can maintain the connection state of the intermediate network without affecting the Application.
Ping and Pong are the heartbeats in websocket, which are used to ensure that the client is online. Generally speaking, only the server sends Ping to the client, and then the client sends Pong to respond, indicating that it is still online.
Websocket is a brand-new, independent protocol, based on the TCP protocol, compatible with the http protocol, but not integrated into the http protocol. A Handshake (Opening Handshake) process also has a Handshake (Closing Handshake) process before closing the connection. After the connection is established, the two parties can communicate in both directions.
WebSocket: data transfer,
After the client and the server are successfully connected, they can communicate. The communication protocol format is WebSocket format. The server uses Tcp Socket to receive data and parse it.
The Websocket protocol transmits data through serialized data frames. Fields such as opcode, payload length, and payload data are defined in the data packet protocol. Which requires:
- The data frame transmitted by the client to the server must be masked: if the server receives a data frame that has not been masked, it must actively close the connection.
- The data frame transmitted by the server to the client must not be masked. If the client receives the masked data frame, it must actively close the connection.
In response to the above situation, the party that finds the error can send a close frame (the status code is 1002, indicating a protocol error) to the other party to close the connection.
The RFC standard of WebSocket will not cause the problem of sticky packets and half packets
The relationship between WebSocket and TCP, HTTP
Like the http protocol, WebSocket is based on TCP, so they are both reliable protocols. The send function of WebSocket called by web developers is ultimately transmitted through the TCP system interface in the browser implementation.
Like the Http protocol, WebSocket belongs to the application layer protocol, so is there any relationship between them? The answer is yes, when WebSocket establishes a handshake connection, the data is transmitted through the http protocol, but after the connection is established, the real data transmission phase does not require the http protocol to participate.
1 This opcode is used to transmit text. Yes, the 1 opcode is for transmitting text (UTF-8). The opcode is set to 0 during the fragmentation process, and 0 is only used for fragmentation. The opcode is 4-bit, and of course there are more than these two values. In addition to specifying the type of transmission data, there are opcodes for other purposes.
A table is also provided in the specification document