Spring websocket sample explanation

scene

Websocket is one of the newly added features of HTML5. Its purpose is to establish a full duplex communication mode between the browser and the server, solve the excessive resource consumption caused by HTTP request response, and provide a new implementation method for special scenario applications, such as chat, stock trading, games and other industries with high real-time requirements.

background

In the browser, only one-way communication can be realized through HTTP, and comet can simulate two-way communication to a certain extent, but it is inefficient and needs better support from the server; Socket and XML socket in flash can realize real two-way communication. These two functions can be used in JavaScript through flex Ajax bridge It can be predicted that if websocket is implemented in the browser, it will replace the above two technologies and be widely used Faced with this situation, HTML5 defines websocket protocol, which can better save server resources and bandwidth and achieve real-time communication. At present, all major mainstream browsers support websocket, and IE browser needs ie10+

1、 POM dependency

POM dependency, spring4 1.4. Release, please add the spring core dependency by yourself. The following is the jar related to websocket

2、 Websocket entry

1. Implement the websocketconfigurer interface and rewrite the registerwebsockethandlers method, which is a core implementation method. Configure the websocket entry, the allowed domain, registered handler, sockjs support and interceptor.

2.registry. Addhandler has the functions of registration and routing. When the client initiates a websocket connection, it gives the / path to the corresponding handler for processing without implementing specific business logic. It can be understood as a collection and task distribution center.

3. Setallowedorigins (string [] domains) allows the specified domain name or IP (including port number) to establish a long connection. If only your own domain name is allowed to access, it can be easily set here. If the "*" sign is used indefinitely, if a domain name is specified, it must start with HTTP or HTTPS.

4. Addinterceptors, as the name suggests, is to add interceptors to the handler. We can add our own logic code before and after calling the handler.

5. Spring websocket also supports stomp protocol, which will be shared next time.

3、 Interceptor implementation

Beforehandshake, process the method before calling handler. It is often used to register user information, bind websocketsession, obtain websocketsession and send messages according to user information in the handler.

4、 Handler processing class

5、 Client connection

Reconnectingwebsocket is used here JS, which adds extensions to the browser's own websocket, such as reconnection, connection timeout, failed reconnection interval, maximum number of attempts to connect, etc.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>