2016-05-17 91 views
0

我正在使用以下代码来创建/订阅主题并处理消息。有时连接丢失,错误说:断开连接时重新连接跺脚

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

哦,上帝,我需要的答案太:( – akiong

回答

0

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

0

这是我使用的聚合物元素是什么:

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) 
    ); 
}, 
相关问题