Spring-supported WebSocket

Source: Internet
Author: User
Tags stomp

Starter: Personal Blog

It must be noted that this blog post is only used for its own records, and the use of spring's websocket support must look at official documents, authoritative and concise, not recommended to look at professional and wordy domestic blog.

Two methods have been tried, of course, regardless of which method you first add Maven dependencies:

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

<dependency>
<groupId>javax.websocket</groupId>
<artifactId>javax.websocket-api</artifactId>
<version>1.0</version>
</dependency>

The first method, corresponding to the 25.2 section of the document:
1) Declare a handler component:

@Component
public class MyHandler extends Textwebsockethandler {

@Override
public void Handletextmessage (websocketsession session, TextMessage message) {
try {
Session.sendmessage (message);
} catch (IOException e) {
E.printstacktrace ();
}
}

}

2) Configure the component in the spring configuration file, and note that it is written in the SPRINGMVC configuration file instead of in the Spring master configuration file:

<!--websocket Configuration 1--
<websocket:handlers>
<websocket:mapping path= "/myhandler" handler= "MyHandler"/>
</websocket:handlers>

Of course, add this configuration to the root node of the XML Beans node to supplement this declaration to let spring know the WebSocket prefix:

xmlns:websocket= "Http://www.springframework.org/schema/websocket"

3) This is OK.


The second method, corresponding to the 25.4 section of the document:
The actual reference is to the official boot document (must be seen), then the Java configuration is changed to XML configuration based on the reference manual.
1) Declare in a controller:

@MessageMapping ("/say")
@SendTo ("/topic/greetings")
public string Greeting (String message) throws Exception {
return message;
}

2) in the SPRINGMVC configuration file, add:

<websocket:message-broker application-destination-prefix= "/app" >
<websocket:stomp-endpoint path= "/hello" >
<websocket:sockjs/>
</websocket:stomp-endpoint>
<websocket:simple-broker prefix= "/topic"/>
</websocket:message-broker>

3) The test page is too long to be posted, find it here, in the lower part of the page.
If you do not bother to look at the official provided sample program in the test page.

Summarize:
The meaning of WebSocket is to keep the browser and server long connected.
If you want to maintain a long connection before websocket, or use a polling method, or a server-side blocking method, it is obvious that performance is wasted.
If the server-side blocking, the programming of the read request (blocking) and write requests (triggered by user action) is also separate.
WebSocket directly separate them, the browser can write instructions to the server at any time, the server can always write instructions to the browser, both have a trigger function to listen to each other.
This is a very important addition to the stateless HTTP protocol.
HTML5 technology is a big aspect is to let B/s bring rich client programming elements, such as local storage, long connect these, this is the rise of the single-page program is an important reason.
Really is a page is a program, has the interface display, has the operation control, has the temporary data, has the persistent data, has the connection which maintains with the server.

Long-term welcome project Cooperation Opportunity Introduction, project income 10% to reward introducer. Sina Weibo: @ Cold Mirror, qq:908789432.

Spring-supported WebSocket

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.