使用sock.js在套接字上踩脚无法与Spring 4 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>
控制器代码:
@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;
}
客户端:
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);
});
});
输出:
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
我做错了什么?
我在google中找到了示例(TickerStocks或类似的东西,问候应用程序(Spring的示例)),所有这些给了我相同的错误。我尝试将WebSocket与握手一起使用(不使用sockjs)-结果相同)。
附加信息:
方法public AjaxResponse hello();
放在根上下文“
/”的IndexController中。因此,我可以提供完整的路径:http://localhost:8080/ws
。部署经过测试的tomcat7和tomcat8。
问题答案:
我遵循Boris Spider的建议,开始使用Java配置(AppConfig和WebInit文件)而不是XML配置文件。当我完成迁移时-我尝试了websockets-可以了!我认为主要问题在于WebSocket的XML配置。