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