2016-04-22 133 views
1

我正在使用「socket.io」:「1.4.5」,「socket.io-redis」:「1.0.0」。使用socket-io-client的javascript客戶端似乎很高興地連接到服務器,除了服務器重新啓動時。連接嘗試失敗,觸發'{「消息」:「xhr輪詢錯誤」,「類型」:「TransportError」,「description」:0「}'消息'connect_error'事件如果我重新啓動瀏覽器連接工作正常。我還注意到,如果我在事件處理程序中爲'connect_error'設置了一個斷點並等待幾秒鐘,那麼連接嘗試最終會成功。我錯過了客戶端代碼中的某個配置設置?這裏是我的客戶端代碼重新啓動服務器後,socket.io javascript客戶端無法重新連接

var socketConnectParams = {'max reconnection attempts': 150}; 
    socketConnectParams['reconnection'] = true; 
    socketConnectParams['reconnectionDelay'] = 5000; 
    socketConnectParams['reconnectionDelayMax'] = 15000; 
    var socket = io('http://localhost:8888', socketConnectParams); 

    socket.on('connect', function() { }); 

    socket.on('connect_error', function(error) { 
     dLog("connection error event received." + JSON.stringify(error)); 
    }); 

回答

3

我聽一個「disconnect」事件重新綁定一個新的'connect'事件。這樣,如果服務器重新啓動,本地客戶端會檢測到這個並創建一個新的監聽器,然後服務器再次啓動。也許你可以試試。

 socket.on('disconnect', function(){ 
      socketCleanup(); // this is a function to cleanup all listeners, just in case, so you can restart fresh 
      socket.on('connect', function(){ 
       socketConnect(); 
      }); 
     }); 
+1

呃,只要沒有關閉選項,socket.io就會自動重新連接。我不知道爲什麼這將是需要的,如果客戶端沒有錯誤配置。 – jfriend00

+0

在我的情況下,套接字確實重新連接 - 但我必須重置連接上發生的所有配置,消息事件(我現在不記得爲什麼)。也許是因爲重新連接得到一個新的套接字ID?我需要檢查並回顧一下我的具體情況。 **編輯**我認爲這是因爲原始套接字訂閱了一個房間,新的套接字將會丟失,需要重新訂閱。 - 我真的需要調查,對不起。 – noderman

相關問題