Step 1: Support asynchronous in web.xml. All filters and servlets
<filter> <filter-name>characterEncoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name></forceEncodingparam-name> <param-value>true</param-value> </init-param> <async-supported>true</async-supported> </filter> <filter-mapping> <filter-name>characterEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Register Central Dispatcher --> < servlet > < servlet-name > springmvc </ servlet-name > <!-- Whatever --> < servlet-class > org.springframework.web.servlet.DispatcherServlet </ servlet-class > <!-- Specify the location and file name of the springmvc configuration file --> < init-param > < param-name > contextConfigLocation </ param-name > < param-value > classpath:applicationContext-mvc.xml </param-value> <!-- springmvc configuration file, classpath represents the class path --> </ init-param > <!-- write a number >0, the smaller the higher the priority (<=0 and no setting are no different ), indicating that the DispatcherServlet object is created when the tomcat server starts --> <!-- When the tomcat starts, the current servlet is created directly --> < load-on-startup > 1 </ load-on- startup > < async-supported > true </ async-supported > </ servlet >
Step two. pom.xml dependencies
<!-- for support web socket --> < dependency > < groupId > javax.websocket </ groupId > < artifactId > javax.websocket-api </ artifactId > < version > 1.1 </ version > < scope > provided </ scope > <!-- Note that the scope must be provided, otherwise the runtime will conflict. If you use tomcat 8, you also need to delete javax.websocket-api.jar under TOMCAT_HOME/lib --> </ dependency > <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.3.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.3.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.3.3</version> </dependency>
third step:
package com.ldr.websocket; import org.springframework.context.annotation.Configuration; import org.springframework.messaging.simp.config.MessageBrokerRegistry; import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer; import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; import org.springframework.web.socket.config.annotation.StompEndpointRegistry; /** * * @author Wang & Yang * @email [email protected] * @date 2018-05-02 * @remark esno company * @version 1.0.0 * @description: * This class represents the domain for enabling websocket message processing and sending and receiving messages config.enableSimpleBroker("/queue", "/topic"); This sentence means that messages can be sent to the client on the two domains of /queue", "/topic; registry.addEndpoint("/endpointChat").withSockJS(); The client connects to the websocket server here */ @Configuration @EnableWebSocketMessageBroker public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer { @Override public void configureMessageBroker(MessageBrokerRegistry config) { config.enableSimpleBroker("/queue", "/topic"); } @Override public void registerStompEndpoints(StompEndpointRegistry registry) { // Register an endpoint named "endpointChat" and specify the SockJS protocol. Peer-to-peer - use registry.addEndpoint("/endpointChat" ).withSockJS(); } }
package com.ldr.websocket; import java.util.HashMap; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.messaging.simp.SimpMessagingTemplate; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import com.ldr.base.GsonBean; @Controller public class WebSocketController { public SimpMessagingTemplate template; @Autowired public WebSocketController(SimpMessagingTemplate template) { this.template = template; } @RequestMapping("/noticeDataGenResult") public void noticeDataGenResult(@RequestParam(value="fn") String fileName,@RequestParam(value="rc") String genResult/*1为成功,0为失败*/) { Map<String,String> datas=new HashMap<String,String>(5); datas.put("fileName", fileName); datas.put("genResult", genResult); template.convertAndSend("/topic/getResponse", new GsonBean(200, datas)); } }
Step 4: jsp
<!-- websock.js --> <script type="text/javascript" src="../sys/js/websock/sockjs.min.js"></script> <script type="text/javascript" src="../sys/js/websock/stomp.min.js"></script> $(function () { connect(); }); function connect() { var sock = new SockJS("../endpointChat"); var stomp = Stomp.over(sock); stomp.connect('guest', 'guest', function(frame) { stomp.subscribe( '/topic/getResponse', function (response) { // Subscribe to the message sent by the /topic/getResponse target. This is defined in @SendTo of the controller. var resultObj= $.parseJSON(response.body ); console.dir(resultObj); }); }); }