Simple application of WebSocket in Springboot _websocket

Source: Internet
Author: User
Using WebSocket in Springboot is different from other ways, mainly because Springboot has a built-in tomcat and a certain configuration for springboot.
The following is a record for using the WebSocket feature in Springboot. First of all, we want to implement a simple function: 1, to achieve a multi-user connection. 2, the client sends the information to the service side. 3, Statistics online number. 4, the server to send information to the client. First, add dependent dependencies in POM
Thymeleaf We just use it here, of course you don't have to add it if you don't use it. Note: 1, if you are using Springboot built-in tomcat to deploy, Pom here is enough, if it is to use the war package to Tomcat deployment, you should also add this package, This is due to the annotations of Java EE to the standard specification (JSR356) supported by WebSocket. It's mainly @serverendpoint. 2, the use of Springboot built-in Tomcat deployment, before writing WebSocket specific implementation class, to inject Serverendpointexporter, This bean is automatically registered to use the WebSocket endpoint of the @serverendpoint annotation declaration. This step is not necessary if the war package is deployed to Tomcat for deployment, as it will be provided and managed by the container itself.

Second, the preparation of service-side specific implementation classes.
Third, write the client. Iv. Compiling statistics pages
Here, the program is basically written, and then we test it. First we use two browsers (Firefox and Google, just as well to test the support of the two browsers for WebSocket) to simulate two user 1: Firefox browser
User 2: Google browser
We use these two users to send messages to the server separately.
Then look at the console output of the server
You can see that the server has successfully received messages from two clients, and this has already achieved the first and second requirements that we mentioned initially. Next we look at the third and fourth requirements. To open our statistics page
As you can see, the number of people online, as well as the people on the line we have counted, we can then choose to send a message to one of them.

As you can see, John successfully received the message. Finally, we can click Send all, send the message to two clients.
Two clients have successfully received the message.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.