Spring 4.0 WebSocket 示例应用程序:无法建立 websocket 连接
Posted
技术标签:
【中文标题】Spring 4.0 WebSocket 示例应用程序:无法建立 websocket 连接【英文标题】:Spring 4.0 WebSocket example-application: Fails to establish websocket connection 【发布时间】:2014-01-28 08:59:43 【问题描述】:我正在尝试启动并运行带有 websockets 的 Spring 4 示例 webapp(在 github 上可用)。但我无法建立 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,因此非常感谢任何帮助解决我的问题。可能这只是一个小问题,因为我可能由于缺乏经验而在配置方面受到监督......
【问题讨论】:
我似乎记得它是强制的,但我会仔细检查。加载网页时,您是否正在通过登录屏幕进行路由?这会建立一个用户会话,并且该用户会获得一个将投资组合更改路由到的通道。 顺便说一句 ...您是否首先与 Jetty 一起工作?该项目配置为使用 Maven Jetty 插件。只需从命令行运行mvn install jetty:run
。
是的。我可以登录。但我的用户没有订阅该频道(如您在日志中所见)。我还没有尝试过 Jetty。正如我所说,我是 Web 开发的新手,所以到目前为止我只使用过 Tomcat……但我肯定会尝试一下。
实际上它与码头一起工作得很好......如果你知道为什么它不能与tomcat一起工作并且你发布了我的问题的答案,我会接受它。谢谢:-)
上面的日志输出显示了一条没有目标订阅者的消息正在发送。用户登录后订阅会发生什么?
【参考方案1】:
这不是一个答案,但它应该能让您在 Tomcat 上使用示例 Web 应用程序启动并运行它,并且可能有助于诊断您自己的 Tomcat 配置。将以下内容放入您的 pom.xml 中 jetty-maven-plugin
配置旁边:
<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 版本或配置并进行比较。
【讨论】:
我会在当天晚些时候看看。谢谢! 在嵌入式 Tomcat 上它也在运行(版本是 7.0.30)......让我感到困惑 我已将 Tomcat maven 插件添加到示例中。该插件的 2.2 版似乎使用的是 Tomcat 7.0.47。【参考方案2】:这可能是由 webapp
和 tomcat
中的 jar 冲突引起的。
如果您在构建中包含任何特定于 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”依赖项,将必要的 jars 放在类路径中进行测试,导入 eclipse等,但不会将它们包含在实际构建的战争中(专门避免这样的情况)
【讨论】:
以上是关于Spring 4.0 WebSocket 示例应用程序:无法建立 websocket 连接的主要内容,如果未能解决你的问题,请参考以下文章
是否可以与 Spring MVC 一起创建 WebSocket 应用程序