急急急!!!Spring WebSocket集成到struts2项目里面去以后请求访问不了
正常的demo里面可以访问到的
1、配置已经扫描到 项目启动服务可以注册
@Configuration
@EnableWebSocket
@EnableWebMvc
public class WebSocketConfig extends WebMvcConfigurerAdapter implements WebSocketConfigurer{
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
//注册两种:ws 和http 实现websocke
System.out.println("注册websocket服务开始------------");
registry.addHandler(systemWebSocketHandler(), "/webSocketServer").addInterceptors(new MyHandshakeInterceptor());
registry.addHandler(systemWebSocketHandler(), "/sockjs/webSocketServer").addInterceptors(new MyHandshakeInterceptor()).withSockJS();
System.out.println("注册websocket服务完成------------");
}
@Bean
public WebSocketHandler systemWebSocketHandler(){
return new SystemWebSocketHandler();
}
}
2. 浏览器支持websocket,用ws = new WebSocket('ws://localhost:7080/webSocketServer');访问时访问不了
报错:
WebSocket connection to 'ws://localhost:7080/webSocketServer' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED
3.application.xml头:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:websocket="http://www.springframework.org/schema/websocket"
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/websocket
http://www.springframework.org/schema/websocket/spring-websocket-4.0.xsd"
default-autowire="byName" default-lazy-init="false">
4.struts.xml添加了:
<constant name="struts.action.excludePattern" value=".*validcode.*,.*webSocketServer*,/webSocketServer,^ws://.$,ws://.*,/webSocketServer.*"/>
5 过滤器里面对url放行了:
if(!uri.equals("/websocket/p2pchat")&& !uri.equals("/webSocketServer")&&!uri.equals("/websocket/p2pchat1")){
//省略
}else{
chain.doFilter(httpRequest, httpResponse);
}
6、web.xml里面struts2配置:
<filter id="filterdispatcher">
<filter-name>Struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Struts2</filter-name>
<url-pattern>*.do</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>