2016-05-17 94 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) 
    ); 
}, 
相關問題