SSM 框架 webSocket的实战

阅读更多
websocket 依赖jar ,直接下载文件。

先看webSocket 服务器端注册处理类:
package info.gy.clk.websocket.config;

import info.gy.clk.websocket.handler.MsgWebSocketHandler;
import info.gy.clk.websocket.interceptor.WebSocketHandshakeInterceptor;

import org.springframework.context.annotation.Bean;
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;

/****
 * 配置websocket
 * @author tmpuser
 *
 */
@Configuration
@EnableWebMvc
@EnableWebSocket
public class WebSocketConfig extends WebMvcConfigurerAdapter implements
		WebSocketConfigurer {

	/**
	 * tomcat7 启动时候会自动注册此方法registerWebSocketHandlers
	 */
	public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
		System.out.println("WebSocketConfig.注册webSocketServer");
		//注册websocket server实现类
		registry.addHandler(msgWebSocketHandler(), "/webSocketServer.pf").addInterceptors(new WebSocketHandshakeInterceptor());
		//访问withSockJS websocket的地址
		registry.addHandler(msgWebSocketHandler(), "/sockjs/webSocketServer.pf").addInterceptors(new WebSocketHandshakeInterceptor()).withSockJS();

	}

	
	@Bean
	public WebSocketHandler msgWebSocketHandler(){
		return new MsgWebSocketHandler();
	}
}


注:
这里的注册地址,要注意下。我这里是.pf 是因为我web.xml中的拦截器是这样配置的
platform
  	
        contextConfigLocation
        classpath:/applicationContext-platform.xml
    
    
	
		platform
		spring mvc 分发容器
		
		org.springframework.web.servlet.DispatcherServlet
		
			contextConfigLocation
			classpath:/platform-servlet.xml
		
		1
		true
	
	
		platform
		*.pf
	

*.pf。如果你们配置的/*那么可以把注册地址.pf去除。我就是被这个拦截器坑了一次。如果对不上访问都是404

前端代码:




WebSocket/SockJSTest

		



    
     


sdfsdf


注:主要是几段JS代码 看看就可以了

package info.gy.clk.control;

import javax.servlet.http.HttpServletRequest;

import info.gy.clk.websocket.handler.MsgWebSocketHandler;

import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.socket.TextMessage;

@Controller
@RequestMapping("/socket")
public class SocketTest {
	
	 	@Bean
	    public MsgWebSocketHandler systemWebSocketHandler() {
	        return new MsgWebSocketHandler();
	    }
	 	
	@RequestMapping("/auditing")
	@ResponseBody
	public void getSocketMsg(HttpServletRequest request){
		systemWebSocketHandler().sendMessageToUser("wf", new TextMessage("测试三三四四"));		
	}
}

这个是服务器端向指定用户端发送消息
-------------------------------------不懂的加 q_q:153 654 8741-----------------------
剩下的几个类自己下载来看看就性
  • 相关jar.rar (3.4 MB)
  • 下载次数: 0
  • 源码.rar (2.7 KB)
  • 下载次数: 0

你可能感兴趣的:(webSocket,spring)