2015-03-30 67 views
2

我注意到,我的節點服務器的net.createConnection()有在某些情況下觸發一個錯誤之前很長的超時(似乎是端口的特定問題...)節點網絡套接字連接超時

我嘗試連接到somedomain:9000(聽,連接和按預期工作) 和某域:1234(同域,不同端口,等待大約2分鐘,直到「連接ETIMEDOUT」)

當我連接到不存在域,我馬上得到一個錯誤,但不是如果我連接到可達主機上的不可訪問的端口。 我需要確定一臺機器是否可以在< 1秒內到達..

我該如何處理?必須通過某種方式在2分鐘內注意到無法訪問的端口? 至少有一種類型的超時,只是在設定的時間之後將地址設置爲不可訪問?

感謝

UPDATE:當前連接代碼:

this.openConnection = function() { 
    try { 
     console.log("[INFO] connecting to " + device.ip + ":" + device.port); 
     device.clientSocket = new net.createConnection(this.port,this.ip) 
      .on('connect', device.connected) 
      .on('data', device.inputReceived) 
      .on('error', function(err) { 

       if (err.code == "ENOTFOUND") { 
        console.log("[ERROR] No device found at this address!"); 
        device.clientSocket.destroy(); 
        return; 
       } 

       if (err.code == "ECONNREFUSED") { 
        console.log("[ERROR] Connection refused! Please check the IP."); 
        device.clientSocket.destroy(); 
        return; 
       } 


       console.log("[CONNECTION] Unexpected error! " + err.message + "  RESTARTING SERVER"); 


       process.exit(1); 

      }) 
      .on('disconnect', function() { 
       console.log("[CONNECTION] disconnected!"); 
      }); 
    } catch(err) { 
     console.log("[CONNECTION] connection failed! " + err); 
    } 
}; 

回答

7

當你連接,你可以設置自己的定時器,你想要的任何超時,如果連接還沒有當計時器觸發成功,那麼它沒有按照你想要的那樣快速成功。

這可以通過單個回調或返回承諾封裝在一個函數中。


基於您的代碼,這裏有一個鏡頭在加上超時它(未測試的代碼):

this.openConnection = function(timeout) { 
    var timer; 
    timeout = timeout || 2000; 
    try { 
     console.log("[INFO] connecting to " + device.ip + ":" + device.port); 
     device.clientSocket = new net.createConnection(this.port,this.ip) 
      .on('connect', function() { 
       clearTimeout(timer); 
       device.connected(); 
      }) 
      .on('data', function() { 
       clearTimeout(timer); 
       device.inputReceived(); 
      }) 
      .on('error', function(err) { 
       clearTimeout(timer); 
       if (err.code == "ENOTFOUND") { 
        console.log("[ERROR] No device found at this address!"); 
        device.clientSocket.destroy(); 
        return; 
       } 

       if (err.code == "ECONNREFUSED") { 
        console.log("[ERROR] Connection refused! Please check the IP."); 
        device.clientSocket.destroy(); 
        return; 
       } 


       console.log("[CONNECTION] Unexpected error! " + err.message + "  RESTARTING SERVER"); 


       process.exit(1); 

      }) 
      .on('disconnect', function() { 
       console.log("[CONNECTION] disconnected!"); 
      }); 
     timer = setTimeout(function() { 
      console.log("[ERROR] Attempt at connection exceeded timeout value"); 
      device.clientSocket.end(); 
     }, timeout); 
    } catch(err) { 
     console.log("[CONNECTION] connection failed! " + err); 
    } 
}; 
+0

我需要設置一個標誌,顯示用戶是否該設備是在線或離線。你會如何做到這一點?我猜,承諾是行不通的,因爲我事先不知道地位會怎樣,也不會錯。 只是一個簡單的setTimeout? – 2015-03-31 09:16:38

+0

@PaulSchneider - 讓我看看你當前的代碼連接你檢測連接失敗的地方,我可以告訴你如何調整它來添加你自己的計時器。 – jfriend00 2015-03-31 13:56:20

+0

這不是很多,但我編輯了上面的代碼:) – 2015-03-31 14:06:28