β

使用spring4的websocket支持

liuxing's blog 241 阅读

J2EE7版(JSR-356)
http://liuxing.info/2015/02/09/websocket-demo

相关依赖请参考上文,spring需要4.x

websocket处理器

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
* 功能说明:websocket连接的拦截器
* 有两种方式
* 一种是实现接口HandshakeInterceptor,实现beforeHandshake和afterHandshake函数
* 一种是继承HttpSessionHandshakeInterceptor,重载beforeHandshake和afterHandshake函数
* 我这里是参照spring官方文档中的继承HttpSessionHandshakeInterceptor的方式
* 作者:liuxing(2015-01-25 03:46)
*/
public class TelWebSocketHandshakeInterceptor extends HttpSessionHandshakeInterceptor {

private static final Logger LOGGER = LoggerFactory.getLogger(TelWebSocketHandshakeInterceptor.class);

/**
* 从请求中获取唯一标记参数,填充到数据传递容器attributes
* @param serverHttpRequest
* @param serverHttpResponse
* @param wsHandler
* @param attributes
* @return
* @throws Exception
*/
@Override
public boolean beforeHandshake(ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
if (getSession(serverHttpRequest) != null) {
ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) serverHttpRequest;
HttpServletRequest request = servletRequest.getServletRequest();
attributes.put("inquiryId", request.getParameter("inquiryId"));
attributes.put("empNo", request.getParameter("empNo"));
}

super.beforeHandshake(serverHttpRequest, serverHttpResponse, wsHandler, attributes);

return true;
}

@Override
public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception ex) {
super.afterHandshake(request, response, wsHandler, ex);
}

private HttpSession getSession(ServerHttpRequest request) {
if (request instanceof ServletServerHttpRequest) {
ServletServerHttpRequest serverRequest = (ServletServerHttpRequest) request;
return serverRequest.getServletRequest().getSession(false);
}
return null;
}

}

session工具类

<!--websocket配置-->
<bean id="telWebSocketHandler" class="包.websocket.handler.TelWebSocketHandler"/>

<websocket:handlers allowed-origins="*">
<websocket:mapping path="webSocketStatus" handler="telWebSocketHandler"/>
<websocket:handshake-interceptors>
<bean class="包.websocket.interceptor.TelWebSocketHandshakeInterceptor"/>
</websocket:handshake-interceptors>
</websocket:handlers>

<bean class="org.springframework.web.socket.server.standard.ServletServerContainerFactoryBean">
<property name="maxTextMessageBufferSize" value="8192"/>
<property name="maxBinaryMessageBufferSize" value="8192"/>
<property name="maxSessionIdleTimeout" value="900000"/>
<property name="asyncSendTimeout" value="5000"/>
</bean>

spring官方文档已经写得很齐全了,更多场景和说明请参阅下文大笑
http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#websocket

J2EE7版(JSR-356)
http://liuxing.info/2015/02/09/websocket-demo

相关依赖请

作者:liuxing's blog
青春须早为,岂能长少年!
原文地址:使用spring4的websocket支持, 感谢原作者分享。

发表评论