重新连接断开的Stomp连接

3
我正在使用以下代码创建/订阅主题并处理消息。有时连接会中断,错误会显示为:
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());
        }
   });

哦天啊,我也需要答案 :( - user2809386
2个回答

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接