我使用Spring WebSockets 4.1.6,Tomcat 7.0.54和Apache 2.4.16作为Web服务器运行一个简单的应用程序时遇到了麻烦。
我在网上看过很多帖子,但我不知道发生了什么。服务器启动没有问题。我在我的服务器项目中发布了一个index.html,它启动了一个WebSocket连接。
如果我直接从Tomcat访问此文件,它可以很好地工作。
http://localhost:8080/myserver/messaging/index.html
但是,如果我从Apache Server访问此文件,它就无法正常工作。
http://localhost/messages/messaging/index.html
在我的Apache服务器中,我配置了代理传递:
ProxyPass /messages http://localhost:8080/myserver
ProxyPassReverse /messages http://localhost:8080/myserver
我的服务器WebSocket配置如下所示:
@Configuration
@EnableWebSocketMessageBroker
public class PuiWebSocketConfig extends
AbstractWebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.setApplicationDestinationPrefixes("/app");
config.enableSimpleBroker("/user", "/topic");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/puimessaging").withSockJS();
}
}
我的客户端按如下方式进行连接:
function connect() {
var socket = new SockJS('/server/puimessaging');
var stompClient = Stomp.over(socket);
var headers = {
puiSessionId : 'a1234567890z'
};
stompClient.connect(headers, function(frame) {
setConnected(true);
stompClient.subscribe('/user/a1234567890z/response', function(data) {
...
});
});
}
服务器抛出错误,说我需要启用异步支持,但我不知道该怎么做。
Async support must be enabled on a servlet and for all filters involved
in async request processing. This is done in Java code using the Servlet
API or by adding "<async-supported>true</async-supported>" to servlet and
filter declarations in web.xml. Also you must use a Servlet 3.0+ container
我尝试在我的web.xml文件中添加该属性,但它不起作用:
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
有什么想法吗?
提前致谢。
马克
答案 0 :(得分:1)
我让它工作,不是通过WebSockets,而是通过xhr_streaming。我的配置如下:
Web.xml中
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="webApp" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
<async-supported>true</async-supported>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Tomcat(server.xml):
<Connector URIEncoding="UTF-8" connectionTimeout="20000"
port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol"
redirectPort="8443" />
Apache httpd:
...
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
LoadModule proxy_express_module modules/mod_proxy_express.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
# LoadModule proxy_html_module modules/mod_proxy_html.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_scgi_module modules/mod_proxy_scgi.so
LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so
...
ProxyPass /pideweb-gijon http://localhost:8080/pideweb_gijon
ProxyPassReverse /pideweb-gijon http://localhost:8080/pideweb_gijon
ProxyPass /pideweb-gijon/endpointpuisocket/ ws://localhost:8080/pideweb_gijon/endpointpuisocket/
ProxyPassReverse /pideweb-gijon/endpointpuisocket/ ws://localhost:8080/pideweb_gijon/endpointpuisocket/
Spring配置:
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Autowired
private WebSocketService websocketService;
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.setApplicationDestinationPrefixes("/app");
config.enableSimpleBroker("/user", "/topic");
// config.setUserDestinationPrefix("/user");
// config.enableSimpleBroker("/queue", "/topic");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/endpointpuisocket").withSockJS();
}
@Override
public void configureClientInboundChannel(ChannelRegistration registration) {
ChannelInterceptorAdapter interceptor = new ChannelInterceptorAdapter() {
@Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {
// store or remove the Session ID in the list depending on the
// message is for Connecting or Disconnecting
StompHeaderAccessor accessor = StompHeaderAccessor
.wrap(message);
StompCommand command = accessor.getCommand();
List<String> headersSessionId = accessor
.getNativeHeader("puiSessionId");
if (!CollectionUtils.isEmpty(headersSessionId)) {
String sessionId = headersSessionId.get(0);
switch (command) {
case CONNECT:
// add the session ID
websocketService.addSession(sessionId);
break;
case DISCONNECT:
// remove the session ID
websocketService.removeSession(sessionId);
break;
default:
break;
}
}
return message;
}
};
registration.setInterceptors(interceptor);
}
}
Javascript客户端:
function(gst) {
var url = '/pideweb-gijon/endpointpuisocket';
var socket = new SockJS(url);
var stompClient = Stomp.over(socket);
var headers = {
puiSessionId : gst
};
var self = this;
stompClient.connect(headers, function(frame) {
console.log('Connected: ' + frame);
self.stompClient.subscribe('/user/' + gst + '/response', function(data) {
console.log(data.body);
});
});
};
这样,当我进入客户端时,会自动尝试连接到服务器。我使用的是Chrome,这是一堆请求:
信息请求
Websocket请求
XHR_streaming请求
有人知道Websocket请求会发生什么吗?
谢谢!
答案 1 :(得分:0)
为此,您需要更新tomcat连接器,进入tomcat目录 - &gt; conf - &gt; server.xml中。找到以下行:
<Connector connectionTimeout="20000" maxThreads="1000" port="8080" protocol="org.apache.coyote.http1.1"/>
它应该是类似的东西,请注意协议,并将其更改为:
<Connector connectionTimeout="20000" maxThreads="1000" port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol"/>
重启你的tomcat。
答案 2 :(得分:0)
知道Tomcat一切正常,Apache配置可能存在问题。
默认情况下,Apache不支持websocket UPGRADE,您需要安装+ configure mod_proxy_wstunnel。
您可能必须确保mod_proxy处于HTTP模式(即不是AJP),并且如果您希望各种SockJS传输工作,则可以正确支持HTTP持久连接。