Java – unable to connect to spring 4 websocket using sockjs stomp over socket

Try using sockjs to use spring 4 websocket with stomp on the socket

My configuration:

websocket. XML – part of the 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:

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 did I do wrong?

I found some examples in Google (tickerstocks or something like that, greeting application (spring example)), all of which gave me the same mistake I tried to use websocket and handshake (no sockjs) – the same result)

Supplementary information:

Method public ajaxresponse hello(); Put in indexcontroller of root context '/' So I can provide the full path: http: / / localhost: 8080 / WS To deploy tested Tomcat 7 and Tomcat 8

Solution

Following Boris the spider's advice, I started using java configuration (AppConfig and webinit files) instead of XML configuration files When I finish the migration – I try WebSockets – it works! I think the main problem is the XML configuration of websocket

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>