Stomp over socket using sockjs can't connect with Spring 4 WebSocket

匿名 (未验证) 提交于 2019-12-03 02:20:02

问题:

Trying to use Spring 4 WebSocket with STOMP over socket using sockjs. And i faced a problem.

My configuration:

websocket.xml - part of spring context

<websocket:message-broker application-destination-prefix="/app">       <websocket:stomp-endpoint path="/ws">                                  <websocket:sockjs/>                                            </websocket:stomp-endpoint>                                        <websocket:simple-broker prefix="/topic"/>                     </websocket:message-broker>        

Controller code:

@MessageMapping("/ws") @SendTo("/topic/ws") public AjaxResponse hello() throws Exception {     AjaxResponse ajaxResponse = new AjaxResponse();     ajaxResponse.setSuccess(true);     ajaxResponse.addSuccessMessage("WEB SOCKET!!! HELL YEAH!");     return ajaxResponse; } 

Client side:

var socket = new SockJS("<c:url value='/ws'/>");                var stompClient = Stomp.over(socket);                              stompClient.connect({}, function(frame) {                              alert('Connected: ' + frame);                                      stompClient.send("/app/ws", {}, {});                            stompClient.subscribe('/topic/ws', function(response){          alert(response.success);                                       });                                                            });                                                                

Output:

Opening Web Socket... stomp.js:130 GET http://localhost:8080/ws/info 404 (Not Found) sockjs-0.3.js:807 Whoops! Lost connection to undefined stomp.js:130 

What i do wrong?

I've found examples in google (TickerStocks or something like that, greeting applications (example of Spring)) and all of them give me the same error. I trying use WebSocket with handshake (without sockjs) - same result).

ADDITION INFORMATION:

Method public AjaxResponse hello(); placed in IndexController on root context "/". So i can provide full path: http://localhost:8080/ws. To deploy tested tomcat7 and tomcat8.

回答1:

I follow Boris The Spider advice and i started use Java Configuration (AppConfig and WebInit files) instead of XML configuration files. When i finished migration - i tried websockets - and it is works! I thought that the main problem was in XML configuration for WebSocket.



回答2:

I think your issue can be the same of this one and its accepted answer can apply.

In short: check how you mapped your DispatcherServlet. For your endpoint URL to be http://localhost:8080/ws the DispatcherServlet and your application contexte root should be set as "/".

It should be something like this, in your web.xml:

<servlet>   <servlet-name>WebSocketDispatcher</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>WebSocketDispatcher</servlet-name>   <url-pattern>/*</url-pattern> </servlet-mapping> 

This or you need to include context-root and dispatcher mapping in your end-point URL:

http://<server>:<port>/<context-root>/<dispatcher-mapping>/ws 

I hope it can help you.

Regards.

Heleno



回答3:

firstly, you have set destination prefix with app, so you must add 'app' into your request url. e.g. if i wanted to request for '/ws', i should create SockJS with path '/server/app/ws', also you can code with var socket = new SockJS("<c:url value='/server/app/ws'/>") and you know the 'server' is your project name over springmvc.

secondly, you must add the path '/app/*' to org.springframework.web.servlet.DispatcherServlet so as to avoid the path with prefix '/app' intercepted by interceptor defined in web.xml.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!