One, spring configuration file Java code
<?xml version= "1.0" encoding= "UTF-8"?>
<beans xmlns= "Http://www.springframework.org/schema/beans"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:tx= "Http://www.springframework.org/schema/tx"
xmlns:context= "Http://www.springframework.org/schema/context"
Xmlns:mvc= "Http://www.springframework.org/schema/mvc"
xmlns:p= "http://www.springframework.org/schema/p"
xmlns:websocket= "Http://www.springframework.org/schema/websocket"
Xsi:schemalocation= "Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
Http://www.springframework.org/schema/tx
Http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-4.1.xsd
Http://www.springframework.org/schema/mvc
Http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
Http://www.springframework.org/schema/websocket
Http://www.springframework.org/schema/websocket/spring-websocket-4.1.xsd ">
<!--@Autowired and other annotations drive
<context:annotation-config/>
<context:component-scan base-package= "Com.soccermaster.websocket" ></context:component-scan>
<context:component-scan base-package= "Com.soccermaster.service" ></context:component-scan>
<context:component-scan base-package= "Com.soccermaster.dao" ></context:component-scan>
<bean class= "Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<bean class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" p:viewclass= " Org.springframework.web.servlet.view.JstlView "
p:prefix= "/web-inf/pages/" p:suffix= ". jsp" >
</bean>
Copy Code
II. Web. XML configuration
Note: The corresponding filter and servlet must be added
Java code
<async-supported>true</async-supported>
Copy Code
<?xml version= "1.0" encoding= "UTF-8"?>
<web-app xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xsi: schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version= " 3.0 ">
<display-name>spring websocket Info Push </display-name>
<welcome-file-list>
<welcome-file>testSocket.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mybatis.xml</param-value>
</context-param>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<async-supported>true</async-supported>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
</web-app>
Copy Code
Third, the Code
A, Websocketconfig
Java code
Package com.soccermaster.websocket;
Import org.springframework.context.annotation.Configuration;
Import ORG.SPRINGFRAMEWORK.WEB.SERVLET.CONFIG.ANNOTATION.ENABLEWEBMVC;
Import Org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
Import Org.springframework.web.socket.WebSocketHandler;
Import Org.springframework.web.socket.config.annotation.EnableWebSocket;
Import Org.springframework.web.socket.config.annotation.WebSocketConfigurer;
Import Org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
Import Org.springframework.context.annotation.Bean;
@Configuration
@EnableWebMvc
@EnableWebSocket
public class Websocketconfig extends Webmvcconfigureradapter implements Websocketconfigurer {
Public Websocketconfig () {
}
@Override
public void Registerwebsockethandlers (Websockethandlerregistry registry) {
Registry.addhandler (Systemwebsockethandler (), "/websck"). Addinterceptors (New Websockethandshakeinterceptor ());
System.out.println ("registed!");
Registry.addhandler (Systemwebsockethandler (), "/sockjs/websck/info"). Addinterceptors (New Websockethandshakeinterceptor ()). WITHSOCKJS ();
}
@Bean
Public Websockethandler Systemwebsockethandler () {
return new Infosocketendpoint ();
return new Systemwebsockethandler ();
}
}
Copy Code
B, Websockethandshakeinterceptor
Java code
Package com.soccermaster.websocket;
Import Java.util.Map;
Import Org.springframework.http.server.ServerHttpRequest;
Import Org.springframework.http.server.ServerHttpResponse;
Import org.springframework.stereotype.Component;
Import Org.springframework.web.socket.WebSocketHandler;
Import Org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;
@Component
public class Websockethandshakeinterceptor extends Httpsessionhandshakeinterceptor {
@Override
public 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);
}
@Override
public void Afterhandshake (ServerHTTPRequest request,
Serverhttpresponse response, Websockethandler Wshandler,
Exception ex) {
SYSTEM.OUT.PRINTLN ("after handshake");
Super.afterhandshake (Request, Response, Wshandler, ex);
}
}
Copy Code
C, Systemwebsockethandler
Java code
Package com.soccermaster.websocket;
Import java.io.IOException;
Import java.util.ArrayList;
Import Org.slf4j.Logger;
Import Org.slf4j.LoggerFactory;
Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.stereotype.Component;
Import Org.springframework.web.socket.CloseStatus;
Import Org.springframework.web.socket.TextMessage;
Import Org.springframework.web.socket.WebSocketHandler;
Import Org.springframework.web.socket.WebSocketMessage;
Import org.springframework.web.socket.WebSocketSession;
<!--The following interface is your own business interface customized to the situation
Import Com.soccermaster.service.IMatchService;
Import Com.soccermaster.util.json.JsonConvert;
/**
*/
@Component
public class Systemwebsockethandler implements Websockethandler {
@Autowired
Private Imatchservice Matchservice;
private static final Logger Logger;
Private static final arraylist<websocketsession> users;
static {
Users = new arraylist<websocketsession> ();
Logger = Loggerfactory.getlogger (Systemwebsockethandler.class);
}
@Override
public void Afterconnectionestablished (websocketsession session)
Throws Exception {
Logger.debug ("Connect to the WebSocket success ...");
SYSTEM.OUT.PRINTLN ("Connect to the WebSocket success ...");
Session.sendmessage (New TextMessage ("server:connected ok!"));
Users.add (session);
}
@Override
public void Handlemessage (websocketsession wss, websocketmessage<?> WSM)
Throws Exception {
Logger.debug ("WebSocket handle Message ...");
TextMessage returnmessage = new TextMessage (result);
Wss.sendmessage ("service-side return information");
Sendmessagetousers (WSS, WSM);
}
@Override
public void Handletransporterror (websocketsession wss, Throwable THRWBL)
Throws Exception {
if (Wss.isopen ()) {
Wss.close ();
}
Users.remove (WSS);
Logger.debug ("WebSocket connection closed happen error ...");
SYSTEM.OUT.PRINTLN ("WebSocket connection closed happen error ...");
}
@Override
public void afterconnectionclosed (websocketsession WSS, Closestatus CS)
Throws Exception {
SYSTEM.OUT.PRINTLN ("WebSocket connection closed ...");
Users.remove (WSS);
Logger.debug ("WebSocket connection closed ...");
}
@Override
public Boolean supportspartialmessages () {
return false;
}
}
Copy Code
Iv. Related Project Jar package
Http://pan.baidu.com/s/1sjI9GOt
As above basic complete spring websocket
Environmental requirements TOMCAT7 and above + JDK 1.7
"The use of Spring+websocket"