多平台统一管理软件接口,如何实现多平台统一管理软件接口
248
2023-05-28
spring框架下websocket的搭建
本文基于Apach Tomcat 8.0.3+MyEclipse+maven+JDK1.7
spring4.0以后加入了对websocket技术的支持,撸主目前的项目用的是SSM(springMVC+spring+MyBatis)框架,所以肯定要首选spring自带的websocket
1 在maven的pom.xml中加入websocket所依赖的jar包
2 更新spring-mvc.xml中namespace.xsd的版本
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:websocket="http://springframework.org/schema/websocket" xsi:schemaLocation=" http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/websocket http://springframework.org/schema/websocket/spring-websocket.xsd"> 处理类和握手协议的spring配置(applicationContext.xml文件) webconfig @Override public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { registry.addHandler(new HelloHandler(), "/hello").addInterceptors(new HandshakeInterceptor()).withSockJS().setHttpMessageCacheSize(20000); } 3 创建握手(handshake)接口 public class HandshakeInterceptor extends HttpSessionHandshakeInterceptor { @Override public http://boolean beforeHandshake(ServerHttpRequest arg0, ServerHttpResponse arg1, WebSocketHandler arg2, Map System.out.println("---- Before Handshake ----"); return super.beforeHandshake(arg0, arg1, arg2, arg3); } @Override public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception ex) { System.out.println("---- After Handshake ----"); super.afterHandshake(request, response, wsHandler, ex); } } 4 创建websocket处理类 public class HelloHandler extends TextWebSocketHandler { public static List static{ users = new ArrayList } @Override public void handleTextMessage(WebSocketSession session, TextMessage message) { //接收到客户端消息时调用 System.out.println("text message: " + session.getId() + "-" + message.getPayload()); } @Override public void afterConnectionEstablished(WebSocketSession session) throws Exception { // 与客户端完成连接后调用 System.out.println("afterConnectionEstablished"); System.out.println("getId:" + session.getId()); System.out.println("getLocalAddress:" + session.getLocalAddress().toString()); System.out.println("getTextMessageSizeLimit:" + session.getTextMessageSizeLimit()); System.out.println("getUri:" + session.getUri().toString()); System.out.println("getPrincipal:" + session.getPrincipal()); System.out.println(soslistService.getsss()); session.sendMessage(new TextMessage("你好")); users.add(session); } @Override public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception { // 消息传输出错时调用 System.out.println("handleTransportError"); } @Override public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception { // 一个客户端连接断开时关闭 System.out.println("afterConnectionClosed"); } @Override public boolean supportsPartialMessages() { // TODO Auto-generated method stub return false; } }
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:websocket="http://springframework.org/schema/websocket"
xsi:schemaLocation="
http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd
http://springframework.org/schema/websocket http://springframework.org/schema/websocket/spring-websocket.xsd">
处理类和握手协议的spring配置(applicationContext.xml文件)
webconfig
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(new HelloHandler(), "/hello").addInterceptors(new HandshakeInterceptor()).withSockJS().setHttpMessageCacheSize(20000);
}
3 创建握手(handshake)接口
public class HandshakeInterceptor extends HttpSessionHandshakeInterceptor {
@Override
public http://boolean beforeHandshake(ServerHttpRequest arg0,
ServerHttpResponse arg1, WebSocketHandler arg2,
Map
System.out.println("---- Before Handshake ----");
return super.beforeHandshake(arg0, arg1, arg2, arg3);
}
@Override
public void afterHandshake(ServerHttpRequest request,
ServerHttpResponse response, WebSocketHandler wsHandler,
Exception ex) {
System.out.println("---- After Handshake ----");
super.afterHandshake(request, response, wsHandler, ex);
}
}
4 创建websocket处理类
public class HelloHandler extends TextWebSocketHandler {
public static List
static{
users = new ArrayList
}
@Override
public void handleTextMessage(WebSocketSession session, TextMessage message) {
//接收到客户端消息时调用
System.out.println("text message: " + session.getId() + "-" + message.getPayload());
}
@Override
public void afterConnectionEstablished(WebSocketSession session)
throws Exception {
// 与客户端完成连接后调用
System.out.println("afterConnectionEstablished");
System.out.println("getId:" + session.getId());
System.out.println("getLocalAddress:" + session.getLocalAddress().toString());
System.out.println("getTextMessageSizeLimit:" + session.getTextMessageSizeLimit());
System.out.println("getUri:" + session.getUri().toString());
System.out.println("getPrincipal:" + session.getPrincipal());
System.out.println(soslistService.getsss());
session.sendMessage(new TextMessage("你好"));
users.add(session);
}
@Override
public void handleTransportError(WebSocketSession session,
Throwable exception) throws Exception {
// 消息传输出错时调用
System.out.println("handleTransportError");
}
@Override
public void afterConnectionClosed(WebSocketSession session,
CloseStatus closeStatus) throws Exception {
// 一个客户端连接断开时关闭
System.out.println("afterConnectionClosed");
}
@Override
public boolean supportsPartialMessages() {
// TODO Auto-generated method stub
return false;
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~