断开连接时重新连接stomp

时间:2016-05-17 14:10:53

标签: websocket activemq stomp

我使用以下代码创建/订阅主题并处理消息。有时连接会丢失并且错误显示:

Whoops! The connection was lost...

我想知道是否有办法重新连接它。是否有可能在错误回调中或在方法中定义整个代码并在错误回调中以递归方式调用它?

 $(document).ready(function () {
  ........
  ...............
      try {
            var socket = new SockJS("${createLink(uri: '/stomp')}");
            var client = Stomp.over(socket);
            client.connect({}, function () {
            client.subscribe("/topic/${userInstance?.username}",                 
            function (message) {
           ............
           ....................

              });
            });
        } catch (error) {
            console.log("ERROR: " + error.toString());
        }
   });

2 个答案:

答案 0 :(得分:1)

我设法使用故障回调并再次连接。只要失败,它就会继续努力。

答案 1 :(得分:0)

这就是我在Polymer元素中使用的内容:

ready: function() {
    this.connectWs();
},
connectWs: function() {
    this.socket = new WebSocket(this.socketUrl);
    this.stompClient = Stomp.over(this.socket);
    this.stompClient.debug = null;
    this.stompClient.connect({},
        function(frame) {
            // Connection OK
        }.bind(this),
        function(e) {
            console.error(e, "Reconnecting WS", this.socketUrl);
            window.setTimeout(function() {
                this.connectWs();
            }.bind(this), 2500);
        }.bind(this)
    );
},
相关问题