Spring 4.0 WebSocket示例 - 应用程序:无法建立websocket连接

时间:2014-01-28 08:59:44

标签: java spring tomcat spring-mvc websocket

我正在尝试使用websockets(在github上提供)启动并运行Spring 4示例webapp。但我无法建立websocket连接。

我在Tomcat-7.0.50上从STS-3.3.0部署,服务器端的一切似乎都很好。成功以用户身份登录后,消息代理正在发布消息。但是客户端无法连接到服务!

1.浏览器中的错误消息(源自sockjs.js):

enter image description here

2.登录请求期间的控制台输出:

18:01:10 [http-bio-8080-exec-10] DispatcherServlet - DispatcherServlet with name 'dispatcher' processing GET request for [/test/]
18:01:10 [http-bio-8080-exec-10] RequestMappingHandlerMapping - Looking up handler method for path /index.html
18:01:10 [http-bio-8080-exec-10] RequestMappingHandlerMapping - Did not find handler method for [/index.html]
18:01:10 [http-bio-8080-exec-10] SimpleUrlHandlerMapping - Matching patterns for request [/index.html] are [/[*][*]]
18:01:10 [http-bio-8080-exec-10] SimpleUrlHandlerMapping - URI Template variables for request [/index.html] are {}
18:01:10 [http-bio-8080-exec-10] SimpleUrlHandlerMapping - Mapping [/index.html] to HandlerExecutionChain with handler [org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler@582e240a] and 1 interceptor
18:01:10 [http-bio-8080-exec-10] DispatcherServlet - Last-Modified value for [/test/] is: -1
18:01:10 [http-bio-8080-exec-10] DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'dispatcher': assuming HandlerAdapter completed request handling
18:01:10 [http-bio-8080-exec-10] DispatcherServlet - Successfully completed request
18:01:10 [http-bio-8080-exec-10] DispatcherServlet - DispatcherServlet with name 'dispatcher' processing GET request for [/test/assets/lib/bootstrap/css/bootstrap.css]
18:01:10 [http-bio-8080-exec-10] RequestMappingHandlerMapping - Looking up handler method for path /assets/lib/bootstrap/css/bootstrap.css
18:01:10 [http-bio-8080-exec-10] RequestMappingHandlerMapping - Did not find handler method for [/assets/lib/bootstrap/css/bootstrap.css]
18:01:10 [http-bio-8080-exec-10] SimpleUrlHandlerMapping - Matching patterns for request [/assets/lib/bootstrap/css/bootstrap.css] are [/[*][*]]
18:01:10 [http-bio-8080-exec-10] SimpleUrlHandlerMapping - URI Template variables for request [/assets/lib/bootstrap/css/bootstrap.css] are {}
18:01:10 [http-bio-8080-exec-10] SimpleUrlHandlerMapping - Mapping [/assets/lib/bootstrap/css/bootstrap.css] to HandlerExecutionChain with handler [org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler@582e240a] and 1 interceptor
18:01:10 [http-bio-8080-exec-10] DispatcherServlet - Last-Modified value for [/test/assets/lib/bootstrap/css/bootstrap.css] is: -1
18:01:10 [http-bio-8080-exec-4] DispatcherServlet - DispatcherServlet with name 'dispatcher' processing GET request for [/test/assets/common/portfolio.css]
18:01:10 [http-bio-8080-exec-4] RequestMappingHandlerMapping - Looking up handler method for path /assets/common/portfolio.css
18:01:10 [http-bio-8080-exec-4] RequestMappingHandlerMapping - Did not find handler method for [/assets/common/portfolio.css]
18:01:10 [http-bio-8080-exec-4] SimpleUrlHandlerMapping - Matching patterns for request [/assets/common/portfolio.css] are [/[*][*]]
18:01:10 [http-bio-8080-exec-4] SimpleUrlHandlerMapping - URI Template variables for request [/assets/common/portfolio.css] are {}
18:01:10 [http-bio-8080-exec-4] SimpleUrlHandlerMapping - Mapping [/assets/common/portfolio.css] to HandlerExecutionChain with handler [org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler@582e240a] and 1 interceptor
18:01:10 [http-bio-8080-exec-4] DispatcherServlet - Last-Modified value for [/test/assets/common/portfolio.css] is: -1
18:01:10 [http-bio-8080-exec-10] DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'dispatcher': assuming HandlerAdapter completed request handling
18:01:10 [http-bio-8080-exec-10] DispatcherServlet - Successfully completed request
18:01:10 [http-bio-8080-exec-4] DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'dispatcher': assuming HandlerAdapter completed request handling
18:01:10 [http-bio-8080-exec-4] DispatcherServlet - Successfully completed request
18:01:10 [http-bio-8080-exec-10] DispatcherServlet - DispatcherServlet with name 'dispatcher' processing GET request for [/test/assets/lib/bootstrap/img/glyphicons-halflings.png]
18:01:10 [http-bio-8080-exec-10] RequestMappingHandlerMapping - Looking up handler method for path /assets/lib/bootstrap/img/glyphicons-halflings.png
18:01:10 [http-bio-8080-exec-10] RequestMappingHandlerMapping - Did not find handler method for [/assets/lib/bootstrap/img/glyphicons-halflings.png]
18:01:10 [http-bio-8080-exec-10] SimpleUrlHandlerMapping - Matching patterns for request [/assets/lib/bootstrap/img/glyphicons-halflings.png] are [/[*][*]]
18:01:10 [http-bio-8080-exec-10] SimpleUrlHandlerMapping - URI Template variables for request [/assets/lib/bootstrap/img/glyphicons-halflings.png] are {}
18:01:10 [http-bio-8080-exec-10] SimpleUrlHandlerMapping - Mapping [/assets/lib/bootstrap/img/glyphicons-halflings.png] to HandlerExecutionChain with handler [org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler@582e240a] and 1 interceptor
18:01:10 [http-bio-8080-exec-10] DispatcherServlet - Last-Modified value for [/test/assets/lib/bootstrap/img/glyphicons-halflings.png] is: -1
18:01:10 [http-bio-8080-exec-10] DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'dispatcher': assuming HandlerAdapter completed request handling
18:01:10 [http-bio-8080-exec-10] DispatcherServlet - Successfully completed request

3.登录请求完成后的3.Console输出:

09:43:36 [MessageBrokerSockJS-1] ExecutorSubscribableChannel - [brokerChannel] sending message id=877832f6-d472-8b43-1cd2-555165a7bf4f
09:43:36 [MessageBrokerSockJS-1] ChannelInterceptorChain - postSend (sent=true) message id 877832f6-d472-8b43-1cd2-555165a7bf4f
09:43:36 [brokerChannel-1] DefaultUserDestinationResolver - Ignoring message to /topic/price.stock.RHT, not a "user" destination
09:43:36 [brokerChannel-1] DefaultSubscriptionRegistry - Found 0 subscriptions for destination=/topic/price.stock.RHT

我首先认为Tomcat是问题,因为我需要阅读7.0.47+版本。但更新并没有产生预期的效果。我对Web开发很陌生,特别是WebSockets,所以非常感谢任何帮助解决我的问题。可能这只是一个小问题,因为由于缺乏经验,我可能会对配置进行监督......

2 个答案:

答案 0 :(得分:1)

这不是一个答案,但它应该让您启动并运行Tomcat上的示例Web应用程序,并可能有助于诊断您自己的Tomcat配置。将以下内容放在jetty-maven-plugin config:

旁边的pom.xml中
<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.0</version>
    <configuration>
        <port>9090</port>
        <path>/${project.artifactId}</path>
    </configuration>
</plugin>

现在从命令行运行:

mvn clean install tomcat7:run

或者在构建时,只需:

mvn tomcat7:run

这将在嵌入式Tomcat 7服务器中运行该应用程序。假设它适合你(它适合我),那么可能值得查看你的Tomcat版本或配置并进行比较。

答案 1 :(得分:1)

这可能是由webapptomcat中存在冲突的广告罐引起的。  如果您在构建中包含任何tomcat或websocket特定的jar并且最终位于{TOMCAT_HOME}/webapps/{YOUR_WEBAPP}/WEB-INF/lib目录中,则它们可能会导致与{TOMCAT_HOME} / lib目录中的jars冲突。请记住,罐子的名称可能不完全相同。我之前遇到了这个确切的问题,并从{TOMCAT_HOME}/webapps/{YOUR_WEBAPP}/WEB-INF/lib directory删除了重复的jar,修复了404错误。

另外请注意,如果您使用gradle使用'war'插件编译webapp,则可以使用'providedCompile'依赖项,将必要的jar放在类路径上进行测试,导入到eclipse中等等,但不会将它们包括在实际的内置战争中(特别是避免这种情况)