【问题标题】:Reconnect stomp when disconnected断开连接时重新连接 stomp
【发布时间】:2017-04-02 00:54:11
【问题描述】:

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

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

【问题讨论】:

  • 天哪,我也需要答案:(

标签: websocket activemq stomp


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    这是我在 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)
        );
    },
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-07
      • 2019-02-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多