Use WebSocket to create chat rooms

One, understand

1. Handshake: The client sends a request to the browser, which is based on Http, but it will identify you as a WebSocket. The server will respond with a 101 status code, and you have established a link. (Based on Http protocol)

2. Data interaction: in this way, the client will actively push messages to the server, and the server will actively push messages to the client, so they are mutual.

Ii. Client (browser implementation client)

Use the following API to create WebSocket objects

let news = new WebSocket(url);
Copy the code

WebSocket event

The event Event handling (that is, when used) describe
open Websocket object. Onopen Triggered when a connection is established
message Websocket object. The onmessage Triggered when the client receives data from the server
error Websocket object. Onerror Triggered when a communication error occurs
close Websocket object. Onclose Triggered when the connection is closed

3. WebSocket method

methods describe
send() Send data using a connection

Third, server implementation

Version 7.0.5 of Tomcat began to support WebSocket and implemented the Java WebSocket specification (JSR356).

Java WebSocket applications consist of a series of WebSocket Tendpoints. An Endpoint is a Java object that represents one end of a WebSocket link. For the server, we can think of an interface that handles specific WebSocket messages, just as servlets do with HTTP requests.

There are two ways to define an Endpoint:

● The first is a program that inherits javax.webSocket. Endpoint and implements its methods.

The second is annotated. You define a POJO (which is a class) and add the @ServerEndpoint annotation (which also configures the resource path).

The Endpoint instance is created during the WebSocket handshake, is valid during the connection between the client and the server, and ends when the link is closed. The methods associated with its lifecycle are clearly defined in the Endpoint interface, and the implementer of the specification ensures that each phase of the lifecycle calls the methods associated with the instance. The lifecycle approach is as follows:

methods To describe annotations
onClose Called when the session is closed @onClose
onOpen This method is called when a new session is started and is called by the client after the handshake between the client and the server is successful @onOpen
onError Called when an exception occurs during connection @onError

How does the server receive the data sent by the client?

By adding a MessageHandler to the session (which is a WebSocket session) to receive messages, we can also specify the method to receive messages using the @onMessage annotation when defining an Endpoint in an annotation.

How does the server push data to the client?

Sending messages is done by RemoteEndpoint, and instances are maintained by Session. Depending on usage, we can use session.getBasicRemote to get the instance where the synchronization message was sent. The message can then be sent by calling its sendXxx () method (the XXX is of type), and the asynchronous message sending instance can be obtained via session.getAsyncremote.