本来用websocket使用注解的方式接受参数,但是有一个需求,有一个参数可能传又可能不传,本人不才,找了很多方式都未能以注解的方式实现,所以还是用传统拦截器的方式获取参数,主要配置如下:
拦截器类
@Component
public class WebSocketInterceptor extends HttpSessionHandshakeInterceptor {
private Logger log = LoggerFactory.getLogger(this.getClass());
/**
* 握手后
*
* @param request
* @param response
* @param wsHandler
* @param ex
*/
@Override
public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
Exception ex) {
}
/**
* TODO 描述该方法的实现功能.
*
* @see org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor#beforeHandshake(org.springframework.http.server.ServerHttpRequest, org.springframework.http.server.ServerHttpResponse, org.springframework.web.socket.WebSocketHandler, java.util.Map)
*/
@Override
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
Map<String, Object> attributes) throws Exception {
if (request instanceof ServletServerHttpRequest) {
ServletServerHttpRequest serverHttpRequest = (ServletServerHttpRequest) request;
//获取参数
String id = serverHttpRequest.getServletRequest().getParameter("id");
String name = serverHttpRequest.getServletRequest().getParameter("name");
log.info("拦截入参{}", id);
attributes.put("id", id);
if (name == null) {
// 如果参数为存0 然后判断这个参数是否为0就可以了
attributes.put("name", "0");
} else {
attributes.put("name", name);
}
HttpSession httpSession = serverHttpRequest.getServletRequest().getSession(false);
if (httpSession != null) {
log.debug("HttpSessionId:" + httpSession.getId());
attributes.put("currHttpSession", httpSession);
} else {
log.debug("httpsession is null");
}
}
return true;
}
@Override
public Collection<String> getAttributeNames() {
// TODO Auto-generated method stub
return super.getAttributeNames();
}
@Override
public boolean isCopyAllAttributes() {
// TODO Auto-generated method stub
return super.isCopyAllAttributes();
}
@Override
public boolean isCopyHttpSessionId() {
// TODO Auto-generated method stub
return super.isCopyHttpSessionId();
}
@Override
public boolean isCreateSession() {
// TODO Auto-generated method stub
return super.isCreateSession();
}
@Override
public void setCopyAllAttributes(boolean copyAllAttributes) {
// TODO Auto-generated method stub
super.setCopyAllAttributes(copyAllAttributes);
}
@Override
public void setCopyHttpSessionId(boolean copyHttpSessionId) {
// TODO Auto-generated method stub
super.setCopyHttpSessionId(copyHttpSessionId);
}
@Override
public void setCreateSession(boolean createSession) {
// TODO Auto-generated method stub
super.setCreateSession(createSession);
}
}
websocket配置类
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
// BidSocketCoreService 这个是websocket接收操作类 路径是与前台一致的路径
registry.addHandler(new BidSocketCoreService(), "/webSocket/socketDetail")
// WebSocketInterceptor就是刚才配置的拦截器类
// setAllowedOrigins("*") 这个要加上 支持跨域
.addInterceptors(new WebSocketInterceptor()).setAllowedOrigins("*");
}
}
接下来BidSocketCoreService类需要继承 WebSocketHandler类 实现接口,类上加上@Service注解,其他的就和注解方式一样了
然后在spring-servlet里加上拦截
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" />
<mvc:exclude-mapping path="/webSocket/**"/>
</mvc:interceptor>
</mvc:interceptors>
就可以了
前端访问ws://xxxxxx/webSocket/socketDetail?id=xxx&name=xxx就可以获取了,name可以传也可以不传
有什么问题互相进步