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();
}
}


[b]注:[/b]
这里的注册地址,要注意下。我这里是.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-----------------------
剩下的几个类自己下载来看看就性
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值