使用sockjs stomp over socket无法与Spring 4 WebSocket连接

dik*_*ini 6 java stomp spring-mvc sockjs spring-websocket

尝试使用sockjs在套接字上使用Spring 4 WebSocket和STOMP.我遇到了一个问题.

我的配置:

websocket.xml - spring上下文的一部分

<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>       
Run Code Online (Sandbox Code Playgroud)

控制器代码:

@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;
}
Run Code Online (Sandbox Code Playgroud)

客户端:

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);                                  
    });                                                           
});                                                               
Run Code Online (Sandbox Code Playgroud)

输出:

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
Run Code Online (Sandbox Code Playgroud)

我做错了什么?

我在谷歌找到了一些例子(TickerStocks或类似的东西,问候应用程序(Spring的例子)),所有这些都给了我同样的错误.我尝试使用WebSocket与握手(没有sockjs) - 相同的结果).

补充资料:

public AjaxResponse hello();放在根上下文"/"的IndexController中的方法.所以我可以提供完整的路径:http://localhost:8080/ws.要部署经过测试的tomcat7和tomcat8.

dik*_*ini 3

我遵循Boris The Spider 的建议,开始使用 Java 配置(AppConfig 和 WebInit 文件)而不是 XML 配置文件。当我完成迁移时 - 我尝试了 websockets - 它有效!我认为主要问题在于 WebSocket 的 XML 配置。