加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 使用sockjs stomp over socket无法连接Spring 4 WebSock

发布时间:2020-12-15 00:36:49 所属栏目:Java 来源:网络整理
导读:尝试使用sockjs在套接字上使用带有STOMP的 Spring 4 WebSocket. 我遇到了一个问题. 我的配置: websocket.xml – spring上下文的一部分 websocket:message-broker application-destination-prefix="/app" websocket:stomp-endpoint path="/ws" websocket:soc
尝试使用sockjs在套接字上使用带有STOMP的 Spring 4 WebSocket.
我遇到了一个问题.

我的配置:

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

我做错了什么?

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

补充资料:

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

解决方法

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读