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中的拦截器是这样配置的
<display-name>platform</display-name>
  	<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:/applicationContext-platform.xml</param-value>
    </context-param>
    
	<servlet>
		<servlet-name>platform</servlet-name>
		<display-name>spring mvc 分发容器</display-name>
		<description></description>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:/platform-servlet.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
		<async-supported>true</async-supported>
	</servlet>
	<servlet-mapping>
		<servlet-name>platform</servlet-name>
		<url-pattern>*.pf</url-pattern>
	</servlet-mapping>

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

前端代码:

<!DOCTYPE html>
<html>
<head>
<title>WebSocket/SockJSTest</title>
<!--[if !IE]> -->
		<script type="text/javascript">
			window.jQuery || document.write("<script src='/gy/platform/themes/ace/assets/js/jquery.js'>"+"<"+"/script>");
		</script>
<!-- <![endif]-->
<!--[if IE]>
<script type="text/javascript">
 window.jQuery || document.write("<script src='/gy/platform/themes/ace/assets/js/jquery1x.js'>"+"<"+"/script>");
</script>
<![endif]-->

    <script src="http://cdn.bootcss.com/sockjs-client/1.1.1/sockjs.min.js"></script>
     <script type="text/javascript">
            var websocket;
            if('WebSocket' in window){
            	websocket=new WebSocket("ws://172.16.71.103/webSocketServer.pf");
            }else if ('MozWebSocket' in window) {
                websocket = new MozWebSocket("ws://172.16.71.103/webSocketServer.pf");
            } else {
                websocket = new SockJS("http://172.16.71.103/sockjs/webSocketServer.pf");
            }
            websocket.onopen = function (evnt) {
            };
            websocket.onmessage = function (evnt) {
                $("#console").html("(<font color='red'>"+evnt.data+"</font>)");
            };
            websocket.onerror = function (evnt) {
            	
            };
            websocket.onclose = function (evnt) {
            	
            }
        </script>
</head>
<body>
<div>    
    <div id="console-container">
        <div id="console">sdfsdf</div>
    </div>
</div>
</body>
</html>


注:主要是几段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-----------------------
剩下的几个类自己下载来看看就性

猜你喜欢

转载自wj-geyuan.iteye.com/blog/2321824