WebSocket to create an online chat room

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)
insert image description here
insert image description here

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:
insert image description here
The handshake from the server looks like the following form:
insert image description here
Field description:
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here

Maven dependency package:

		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
        </dependency>

Guess you like

Origin blog.csdn.net/m0_57249797/article/details/122940394