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

dikkini picture dikkini · Mar 18, 2014 · Viewed 23.3k times · Source

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.

Answer

dikkini picture dikkini · Apr 18, 2014

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.