我正在尝试使用 websockets(可在 github 上获得)启动并运行 Spring 4 示例 webapp。但是我无法建立 websocket 连接。
我在 Tomcat-7.0.50 上从 STS-3.3.0 进行部署,服务器端似乎一切正常。以用户身份成功登录后,消息代理正在发布 消息。但是客户端无法连接到该服务!
1.浏览器报错(源自sockjs.js):
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.登录请求完成后控制台输出:
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,因此非常感谢任何帮助解决我的问题的人。可能这只是一个小问题,因为我可能由于缺乏经验而在配置中监督了一些东西......
最佳答案
这不是一个答案,但它应该让您在 Tomcat 上启动并运行示例 Web 应用程序,并且可能有助于诊断您自己的 Tomcat 配置。将以下内容放在 jetty-maven-plugin
配置旁边的 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 版本或配置并进行比较。
关于java - Spring 4.0 WebSocket 示例应用程序 : Fails to establish websocket connection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21401005/