使用sockjs使用Spring 4 WebSocket无法连接套接字 [英] Stomp over socket using sockjs can't connect with Spring 4 WebSocket
问题描述
尝试使用sockjs在套接字上使用带有STOMP的Spring 4 WebSocket。
我遇到了问题。
Trying to use Spring 4 WebSocket with STOMP over socket using sockjs. And i faced a problem.
我的配置:
websocket.xml - 春天的一部分上下文
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>
控制器代码:
@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) - 相同的结果)。
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).
附加信息:
方法 public AjaxResponse hello();
放在根上下文/的IndexController中。所以我可以提供完整路径: http:// localhost:8080 / ws
。
部署经过测试的tomcat7和tomcat8。
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.
推荐答案
我按照 Boris The Spider 建议我开始使用Java配置(AppConfig和WebInit文件)而不是XML配置文件。当我完成迁移 - 我尝试了websockets - 它是有效的!我认为主要的问题是WebSocket的XML配置。
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.
这篇关于使用sockjs使用Spring 4 WebSocket无法连接套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!