简体   繁体   English

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

[英]Spring 4.0 WebSocket example-application: Fails to establish websocket connection

I am trying to get the Spring 4 example webapp with websockets (available on github ) up and running. 我正在尝试启动并运行带有websockets(可在github上 )的Spring 4示例Webapp。 But I cannot get a websocket connection established. 但是我无法建立Websocket连接。

I am deploying from STS-3.3.0 on a Tomcat-7.0.50 and everything seems to be fine on the server side. 我正在从Tomcat-7.0.50上的STS-3.3.0进行部署,并且在服务器端一切正常。 After having been successfully logged in as a User, the message broker is publishing messages. 成功以用户身份登录后, 消息代理正在发布消息。 But the client fails to connect to the service! 但是客户端无法连接到服务!

1.Error message in the browser (originating from sockjs.js): 1.浏览器中的错误消息(源自sockjs.js):

在此处输入图片说明

2.Console output during login request: 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.Console output after login request is completed: 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

I first thought Tomcat was the problem as I read version 7.0.47+ is required. 我最初以为Tomcat是问题所在,因为我需要阅读7.0.47+版本。 But updating didnt have the desired effect. 但是更新并没有达到预期的效果。 I am quite new to Web-development, especially to WebSockets, so any help to solve my issue is greatly appreciated. 我对Web开发特别是WebSockets还是很陌生,因此非常感谢您能解决我的问题。 Probably it's only a minor problem as i might have something overseen in configuration due to lacking experience ... 可能这只是一个小问题,因为由于经验不足,我可能会对配置进行一些监督...

This is not an answer as such, but it should get you up and running with the example web app on Tomcat and may help diagnose your own Tomcat config. 这不是一个答案,但是它可以使您在Tomcat上使用示例Web应用程序并开始运行,并且可能有助于诊断您自己的Tomcat配置。 Put the following in your pom.xml next to the jetty-maven-plugin config: 将以下内容放在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>

Now from the command-line, you can run: 现在,从命令行,您可以运行:

mvn clean install tomcat7:run

or when it has been built, just: 或在构建后,只需:

mvn tomcat7:run

This will run the application in an embedded Tomcat 7 server. 这将在嵌入式Tomcat 7服务器中运行该应用程序。 Assuming that works for you (it does for me), then it may be worth looking at your Tomcat version or config and comparing. 假设对您有效(对我而言有效),那么可能值得查看您的Tomcat版本或配置并进行比较。

This is potentially caused by conflicting jars in your webapp and tomcat . 这可能是由于webapptomcat jar冲突引起的。 If you have any tomcat or websocket specific jars that you are including in your build and that end up in the {TOMCAT_HOME}/webapps/{YOUR_WEBAPP}/WEB-INF/lib directory, they may be causing the conflict with jars in the {TOMCAT_HOME}/lib directory. 如果您有任何Tomcat或WebSocket的具体罐子要包括在你的构建,并在那最终{TOMCAT_HOME}/webapps/{YOUR_WEBAPP}/WEB-INF/lib的目录,它们可能会导致与冲突jars在{ TOMCAT_HOME} / lib目录。 Keep in mind that the jars might not be named precisely the same. 请记住,罐子的名称可能不完全相同。 I experienced this exact problem before and removing the duplicate jars from the {TOMCAT_HOME}/webapps/{YOUR_WEBAPP}/WEB-INF/lib directory fixed the 404 error. 我之前遇到过这个确切的问题,并且从{TOMCAT_HOME}/webapps/{YOUR_WEBAPP}/WEB-INF/lib directory删除了重复的jar,修复了404错误。

As a further note, if you are using gradle to compile your webapp with the 'war' plugin, there is a 'providedCompile' dependency that you can use that will put the necessary jars on your classpath for testing, importing into eclipse, etc. but will not include them in the actual built war (to specifically avoid scenarios like this) 进一步说明,如果您使用gradle通过“ war”插件编译Web应用程序,则可以使用“ providedCompile”依赖项,该依赖项会将必要的jar放在类路径中进行测试,导入eclipse等。但不会将它们包括在实际的内战中(以特别避免此类情况)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM