SpringBoot integrates WebSocket for data push

  • The basic concept
  • WebSocket sample

The basic concept

  • WebSocket:

    • WebSocket is a protocol for full duplex communication over a single TCP connection
    • WebSocket makes data exchange between server and client easier, allowing the server to actively push data to the client
    • In WebSocket, the browser and server only need to complete a handshake, and the two can directly create a persistent connection and two-way data transfer
  • useWebSocketTo achieve data push:

  • Advantages of using WebSocket to implement data push:

    • Less control overhead: When data is exchanged between the server and client after the connection is created, the packet headers used for protocol control are relatively small. Without extensions, headers are only 2-10 bytes in size for server-to-client content, depending on packet length; For client-to-server content, an additional 4-byte mask is added to the header. This is significantly less overhead than the need to carry a full header each time an HTTP request is made
    • Strong real-time performance: Because the protocol is full-duplex, the server can actively send data to the client at any time. Compared with HTTP requests that require a client to initiate a request before the server can respond, the latency is significantly reduced. It also delivers data many more times in a short period of time than long-length polling like Comet
    • Keepalive a connection: Unlike HTTP,WebSocket needs to create a connection first, which makes WebSocket a stateful protocol that allows communication to omit some of the state information. HTTP requests, on the other hand, may require status information, such as authentication, to be carried with each request
    • Good binary support: Binary frames are defined in WebSocket. Binary data can be handled better than HTTP
    • Support for extensions: WebSocket defines extensions that can extend protocols and implement some custom sub-protocols. For example, some browsers support compression
    • Better compression: Compared to HTTP compression,WebSocket, with appropriate extension support, can use the previous context, which can significantly improve compression when passing similar data

WebSocket sample

  • PC:

    • Log in to the system administrator using the account and password
    • Server Verify the Server permission framework
    • Check success
    • Save sessions by user ID
    • The login page automatically connects to WebSocket
    • Server The Server establishes a connection and binds the current browser Session based on the user ID
  • APP:

    • Fill in the information at the APP end
    • Server Server Check whether the Server verification information is correct
    • Check success
    • Server queries whether the administrator is online
    • Querying an Administrator Online
    • Push information A message filled in
  • Note:

    • If you exit the PC system, close the WebSocket connection
    • If the APP information is filled in successfully, check whether the administrator is online. If online, push the message of filling in successfully. If the administrator is offline, save the message and push the message when the administrator goes online
  • WebSocket example