The construction of Spring-websocket

Source: Internet
Author: User

Apach Tomcat 8.0.3+myeclipse+maven+jdk1.7

Spring4.0 later joined the support for WebSocket technology, the main current project is the SSM (springmvc+spring+mybatis) box

WebSocket, so be sure to prefer Spring's own

1 Add the jar package that WebSocket relies on in Maven Pom.xml

<Dependency> <Groupid>com.fasterxml.jackson.core</Groupid> <Artifactid>jackson-core</Artifactid> <version>2.4.0</Version></Dependency><Dependency> <Groupid>com.fasterxml.jackson.core</Groupid> <Artifactid>jackson-databind</Artifactid> <version>2.4.0</Version></Dependency><Dependency><Groupid>org.springframework</Groupid><Artifactid>spring-websocket</artifactid> <version> 4.1.6.release</version></ Dependency><dependency> < groupid>org.springframework</groupid> <artifactid>spring-messaging</ artifactid> <version>4.1.6.release</version></DEPENDENCY> 
2 Update version namespace.xsd in Web. XML
/span>
<beans xmlns=< Span class= "value" > "Http://www.springframework.org/schema/beans"   Xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"  xmlns:websocket= "Http://www.springframework.org/schema/websocket" Span class= "indent" > xsi:schemalocation= "HTTP +/ Www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd   Http://www.springframework.org/schema/websocket/http Www.springframework.org/schema/websocket/spring-websocket.xsd 

handles the spring configuration of the class and the Handshake Protocol ( Applicationcontext.xml file)
<bean id= "WebSocket" class= "Com.xl.websocket.WebsocketEndPoint"/><< Span class= "title" >websocket:handlers> <websocket:mapping path= "/websocket" handler= " WebSocket "/> <websocket:handshake-interceptors> <bean class= " Com.up.websocket.HandshakeInterceptor "/> </websocket: Handshake-interceptors></WEBSOCKET:HANDLERS>    
3 Creating a handshake (handshake) interface
PublicClassHandshakeinterceptorExtendshttpsessionhandshakeinterceptor{ @Override PublicBoolean Beforehandshake (ServerHTTPRequest request,  Serverhttpresponse response, Websockethandler Wshandler,  map<string, object> attributes)Throws Exception { System.out.println ("Before handshake");  return  Super.beforehandshake (Request, response, Wshandler, attributes); }  @Override  public void afterhandshake (serverhttprequest request,   Serverhttpresponse response, Websockethandler wshandler,   Exception ex) {  System.out.println ( "after handshake");  super.afterhandshake (request, Response, Wshandler, ex);                 

4 Creating a WebSocket processing class

Publicclass websocketendpoint extends textwebsockethandler {  @Override  protected void handletextmessage ( Websocketsession session,   TextMessage message) span class= "keyword" >throws Exception {   Super.handletextmessage (session, message);   TextMessage ReturnMessage = new textmessage ( Message.getpayload () + "received");   Session.sendmessage (returnmessage); }}    
 < span class= "keyword" > &NBSP;     
  < Span class= "indent" >< Span class= "value" >&NBSP;             
< span class= "tag" > < Span class= "title" > < span class= "tag" > &NBSP;     

Spring-websocket the build

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.