Java-websocket the building is easy. The child useless framework can be downloaded here in the main line and the individual teaching good Java-websocket program:
Apach Tomcat 8.0.3+myeclipse+maven+jdk1.7:
http://download.csdn.net/detail/up19910522/7719087
Spring4.0 later added support for WebSocket technology, and the current project with the master is the SSM (springmvc+spring+mybatis) box
Frame, the in order to be sure to prefer spring comes with websocket, well, now the problem, and the main in the online various searches fierce search, pieced together a few self-proclaimed is a
Spring the WebSocket. Down a look, punk. Contains the down from GitHub. Give me a sample example. There is a problem in the building process,
Lord Google search, a total of three pages results total 30 or so , the first 15 articles are in plain English and the last 15 are Korean and Japanese, and none of the 30 results
resolution of the masturbate The main problem, helpless, just good officer net to see the whole English help, in the master of the amazing perseverance and tireless struggle. Tangled up the spring+websocket for two days
Integration Today The Day is a complete build success, groping thoroughly.
WebSocket is the only Internet technology that truly enables full-duplex communications Server-to-client, compared to long-connection and polling technologies,
The superiority of websocket is self-evident, long-connected connection resources (thread resources) increase with the number of connections. Must be exhausted. Client polling will give the service
WebSocket is a long, client-to-server connection at the physical layer non-network level, which ensures that the server
Instant push from the user without consuming thread resources and not continually polling requests to the server.
The following is a good way to talk about the twists and turns of the Springmvc+spring+mybatis integrated WebSocket technology in the SSM framework, until the path of success and joy.
- 1 Add the jar package that WebSocket depends on in Maven pom.xml, what, you don't know maven, go for it or check out the main Maven's blog is a bad patch, and Spring-websocket relies on the following jar packages:
<dependency> <groupId>javax.servlet</groupId> <artifactid>javax.servlet-api</ Artifactid> <version>3.1.0</version></dependency><dependency> <groupId> Com.fasterxml.jackson.core</groupid> <artifactId>jackson-core</artifactId> <version> 2.3.0</version></dependency><dependency> <groupid>com.fasterxml.jackson.core</ Groupid> <artifactId>jackson-databind</artifactId> <version>2.3.0</version></ Dependency><dependency> <groupId>org.springframework</groupId> <artifactId> Spring-websocket</artifactid> <version>4.0.1.RELEASE</version></dependency>< Dependency> <groupId>org.springframework</groupId> <artifactid>spring-messaging</ Artifactid> <version>4.0.1.RELEASE</version></dependency>
- 2 Update the version number of namespace.xsd in Web. Xml.
<beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance " xmlns:websocket=" Http://www.springframework.org/schema/websocket " xsi: schemalocation= " Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans.xsdhttp://www.springframework.org/schema/websocket Http://www.springframework.org/schema/websocket /spring-websocket.xsd ">
- 3 Update the Spring Framework jar package to more than 4.0 (Spring-core, Spring-context, Spring-web and SPRING-WEBMVC)
<dependency><span style= "White-space:pre" ></span><groupid>org.springframework</ Groupid><artifactid>spring-core</artifactid><version>${spring.version}</version> </dependency><dependency><groupId>org.springframework</groupId><artifactId> Spring-web</artifactid><version>${spring.version}</version></dependency><dependency ><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId>< Version>${spring.version}</version></dependency><dependency><groupid> org.springframework</groupid><artifactid>spring-context-support</artifactid><version>$ {spring.version}</version></dependency>
- 4 4.1 Creating a WebSocket processing class
Package Com.up.websocket.handler;import Org.springframework.web.socket.textmessage;import Org.springframework.web.socket.websocketsession;import Org.springframework.web.socket.handler.textwebsockethandler;public class Websocketendpoint extends Textwebsockethandler {@Overrideprotected void Handletextmessage (websocketsession session,textmessage message) throws Exception {Super.handletextmessage (session, message); TextMessage returnmessage = new TextMessage (message.getpayload () + "received at server"); Session.sendmessage ( returnmessage);}}
- 4.2 Creating a handshake (handshake) interface
package Com.up.websocket;import Java.util.map;import Org.springframework.http.server.serverhttprequest;import Org.springframework.http.server.serverhttpresponse;import Org.springframework.web.socket.WebSocketHandler; Import Org.springframework.web.socket.server.support.httpsessionhandshakeinterceptor;public Class Handshakeinterceptor extends httpsessionhandshakeinterceptor{@Overridepublic boolean 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);} @Overridepublic void Afterhandshake (serverhttprequest request,serverhttpresponse response, Websockethandler Wshandler,exception ex) {System.out.println ("after handshake"); Super.afterhandshake (request, Response, Wshandler, ex );}}
- 5 Spring configuration for processing class and handshake protocol (applicationcontext.xml file)
<bean id= "WebSocket" class= "Com.up.websocket.handler.WebsocketEndPoint"/><websocket:handlers> <websocket:mapping path= "/websocket" handler= "WebSocket"/> <websocket:handshake-interceptors> <bean class= "Com.up.websocket.HandshakeInterceptor"/> </websocket:handshake-interceptors> </websocket:handlers>
<! DOCTYPE html>
New Sockjs (url, undefined, {protocols_whitelist:transports}): New WebSocket (URL); Ws.onopen = function () {setconnected (true); Log (' info:connection opened. '); }; Ws.onmessage = function (event) {log (' Received: ' + event.data); }; Ws.onclose = function (event) {setconnected (false); Log (' info:connection closed. '); Log (event); }; } function Disconnect () {if (ws! = null) {ws.close (); WS = NULL; } setconnected (FALSE); } function Echo () {if (ws! = null) {var message = document.getElementById (' message '). VA Lue Log (' Sent: ' + message); Ws.send (message); } else {alert (' connection not established, please connect. '); } } function UpdateUrl (URLPath) {if (Urlpath.indexof (' sockjs ')! =-1) {url = URLPath; document.getElementById (' Sockjstransportselect '). style.visibility = ' visible '; } else {if (Window.location.protocol = = ' http: ') {url = ' ws://' + Window.locati On.host + URLPath; } else {url = ' wss://' + window.location.host + urlpath; } document.getElementById (' Sockjstransportselect '). style.visibility = ' hidden '; }} function Updatetransport (transport) {alert (transport); Transports = (Transport = = ' all ')? []: [Transport]; } function log (message) {var console = document.getElementById (' console '); var p = document.createelement (' P '); P.style.wordwrap = ' Break-word '; P.appendchild (document.createTextNode (message)); Console.appendchild (P); while (Console.childNodes.length >) {console.removechild (console.firstchild); } console.scrolltop = Console.scrollheight; } </script>
- 7 According to the above steps to build, according to individual development environment is different. May be a variety of problems, the following will be in the entire construction process encountered problems summed up, see blog: http://blog.csdn.net/gisredevelopment/article/details/38397569
Demo instance enhanced Download: Spring-websocket-uptest.rar
respect for originality. Reprint please specify the Source:
http://blog.csdn.net/gisredevelopment/article/details/38392629
Technical Email, free answer: [email protected]
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
Spring+websocket synthesis (Springmvc+spring+mybatis This is the SSM framework and WebSocket integration technology)