Introduction to WebSocket
WebSocket is a network communication protocol . RFC6455 defines its communication standard.
WebSocket is a protocol provided by HTML5 for full-duplex communication on a single TCP connection.
HTTP protocol is a stateless, connectionless, one-way application layer protocol. It uses a request/response model. Communication requests can only be initiated by the client, and the server responds to the request.
This communication model has a drawback: the HTTP protocol cannot enable the server to actively initiate messages to the client.
The characteristics of this one-way request are destined to be very troublesome for the client to know if the server has continuous state changes. Most web applications will implement long polling with frequent asynchronous AJAX requests. The efficiency of polling is low, which is a waste of resources (because it must be connected continuously, or the HTTP connection is always open)
WebSocket protocol
This protocol has two parts: handshake and data transmission
The handshake is based on the http protocol
The handshake from the client looks like the following form:
The handshake from the server looks like the following form:
Field description:
Maven dependency package:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>