2013-09-21 232 views
9

我想找出一種方法讓我的SockJS客戶端重新連接到服務器,如果它應該停止。SockJS客戶端自動重新連接?

目前,我有這樣的:

new_conn = function() {  
     socket = new SockJS(protocol + serverDomain + '/echo', null, { 
      'protocols_whitelist': ['websocket', 'xdr-streaming', 'xhr-streaming', 'iframe-eventsource', 'iframe-htmlfile', 'xdr-polling', 'xhr-polling', 'iframe-xhr-polling', 'jsonp-polling'] 
     }); 
    }; 

    socket.onopen = function() { 
     clearInterval(recInterval); 
    }; 

    socket.onclose = function() {  
     recInterval = window.setInterval(function() { 
      new_conn(); 
     }, 2000); 
    }; 

的問題是,setInterval保持一個成功的重新連接後甚至射擊。看起來socket.onopen從來沒有得到執行。

任何想法我可能做錯了什麼?

+2

,使用的setTimeout()應該比的setInterval() – Fielding34

+0

@ Fielding34其實我認爲這是完全相反的更好 - '的setInterval()'是更好,因爲OP希望保持嘗試(服務器關閉時),直到它連接 – TMG

回答

10

我認爲這可能與變量範圍有關。試試這個:

var recInterval = null; 

new_conn = function() {  
    socket = new SockJS(protocol + serverDomain + '/echo', null, { 
     'protocols_whitelist': ['websocket', 'xdr-streaming', 'xhr-streaming', 'iframe-eventsource', 'iframe-htmlfile', 'xdr-polling', 'xhr-polling', 'iframe-xhr-polling', 'jsonp-polling'] 
    }); 
}; 

socket.onopen = function() { 
    clearInterval(recInterval); 
}; 

socket.onclose = function() {  
    recInterval = window.setInterval(function() { 
     new_conn(); 
    }, 2000); 
}; 

不管怎麼說,這是奇怪的,因爲你的window對象上宣佈recInterval,它應該有工作。如果不起作用,您還可以使用瀏覽器進行調試,使用debugger;語句或者通過設置本地斷點...(例如在onopen中)進行交互式調試。

順便說一句,我rewrited整個這樣的代碼(我喜歡重構:):

var recInterval = null; 
var socket = null; 

var new_conn = function() {  
    socket = new SockJS(protocol + serverDomain + '/echo', null, { 
     'protocols_whitelist': ['websocket', 'xdr-streaming', 'xhr-streaming', 
           'iframe-eventsource', 'iframe-htmlfile', 
           'xdr-polling', 'xhr-polling', 'iframe-xhr-polling', 
           'jsonp-polling'] 
    }); 

    socket.onopen = function() { 
     clearInterval(recInterval); 
    }; 

    socket.onclose = function() {  
     recInterval = setInterval(function() { 
      new_conn(); 
     }, 2000); 
    }; 
}; 
6

在任何情況下,本主題中仍然有興趣:從franzlorenzon重構後的代碼片段會導致很多重新連接的自它是一種遞歸重新連接,因爲每兩秒鐘就會產生一個新的onclose事件(不管recInterval如何)。

在套接字創建之後立即移動清除間隔就行了。我還在onclose事件中添加了一個socket = null。

var recInterval = null; 
 
var socket = null; 
 

 
var new_conn = function() { 
 
    socket = new SockJS(protocol + serverDomain + '/echo', null, { 
 
    'protocols_whitelist': ['websocket', 'xdr-streaming', 'xhr-streaming', 
 
     'iframe-eventsource', 'iframe-htmlfile', 
 
     'xdr-polling', 'xhr-polling', 'iframe-xhr-polling', 
 
     'jsonp-polling' 
 
    ] 
 
    }); 
 

 
    clearInterval(recInterval); 
 

 
    socket.onopen = function() { 
 

 
    }; 
 

 
    socket.onclose = function() { 
 
    socket = null; 
 
    recInterval = setInterval(function() { 
 
     new_conn(); 
 
    }, 2000); 
 
    }; 
 
};

你的情況
+1

感謝分享。在重新連接嘗試之間的時間增加的情況下建立退避協議也可能會很好。然後,如果用戶通過監視應用程序中的滾動來「挫敗」,則放棄重新連接時間。很明顯,他們會通過重新加載頁面來觸發重新連接嘗試。 – coding

+0

@coding是的,你是對的。上面的代碼只是一個修正前面例子中的錯誤的例子。我們正在使用您在我們的生產系統中建議的解決方案,並且工作得很好。此外,我正在爲私人項目使用類似的解決方案。沒有併發症。 :) – daPhantom

相關問題