Websocket 断开时重新连接跺脚

Websocket 断开时重新连接跺脚,websocket,activemq,stomp,Websocket,Activemq,Stomp,我使用以下代码创建/订阅主题并处理消息。有时连接丢失,错误显示: Whoops! The connection was lost... 我想知道是否有办法重新连接它。是否可以在错误回调中定义整个代码,或者在错误回调中递归调用整个代码 $(document).ready(function () { ........ ............... try { var socket = new SockJS("${createLink(uri: '/s

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

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());
        }
   });

我成功地使用失败回调并再次连接。只要失败,它就会继续尝试。

这就是我在聚合物元件中使用的:

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)
    );
},

哦,上帝,我也需要答案:(