2014-03-07 73 views
1

Helo, 我在Windows Azure Mobile服務中創建API,在此api腳本中,我具有連接其他服務的功能。我有問題如何返回值或停止執行我的腳本時,我有良好的服務答案。函數process.exit(1)不起作用。返回值或停止腳本

function function1(item,response) { 
    var buf =''; 
    var net = require('net'); 
    var HOST = 'xxxx.xxxx.xxxx.xxxx'; 
    var PORT = xxx; 

    var client = new net.Socket(); 

    client.setTimeout(100000, function() { 
     console.log("Timeout"); 
     response.send(500, "Timeout"); 
    }); 

    client.connect(PORT, HOST, function() { 
     client.write(item + "\n"); 
     client.on('data', function(data) { 
      buf = buf + data.toString('utf-8');   
     }); 

     client.on('close', function() { 
     }); 

     client.on('end', function() { 
      if (buf.length > 1) {  
        var result = JSON.parse(buf); 
        //if resulr.Aviable is true the functios should return result or  send result and stop execiuting script 
        if (result.Avaiable) { 
         response.send(200, result); 
         //now i wont't to respond answer to client or return my value(result) 
         console.log('Send data');       
        } 
      }   
      client.destroy();    
     });          
    }); 

    } 
+0

你在代碼中有response.send,是不是返回你的結果?什麼沒有發生,你期望發生? – MikeWo

+0

但如果我將'response.send'更改爲'return result'。函數返回未定義的參數。 – user3125146

回答

0

一種替代方法是有一個標誌,指示是否已發送響應。這樣,當到達第一個選項時,可以將該標誌設置爲true(可能清除超時值,使其不會超過其所需),並且在所有情況下檢查該標誌是否已設置,然後再返回響應。沿着下面的代碼的東西線:

function function1(item,response) { 
    var buf = ''; 
    var net = require('net'); 
    var HOST = 'xxxx.xxxx.xxxx.xxxx'; 
    var PORT = xxx; 

    var client = new net.Socket(); 

    var responseSent = false; 

    var timeoutHandler = client.setTimeout(100000, function() { 
     if (!responseSent) { 
      responseSent = true; 
      console.log("Timeout"); 
      response.send(500, { error: "Timeout" }); 
     } 
    }); 

    client.connect(PORT, HOST, function() { 
     client.write(item + "\n"); 
     client.on('data', function(data) { 
      buf = buf + data.toString('utf-8');   
     }); 

     client.on('close', function(had_error) { 
      if (!responseSent) { 
       clearTimeout(timeoutHandler); 
       responseSent = true; 
       console.log('Socket closed'); 
       response.send(500, { error: had_error ? 'Socket error' : 'unknown' }); 
      } 
     }); 

     client.on('end', function() { 
      if (!responseSent) { 
       responseSent = true; 
       clearTimeout(timeoutHandler); 
       if (buf.length > 1) { 
        try { 
         var result = JSON.parse(buf); 
         if (result.Available) { 
          response.send(200, result); 
         } else { 
          response.send(500, { error: 'Socket data is not available' }); 
         } 
        } catch (ex) { 
         response.send(500, { error: 'error parsing JSON', exception: ex }); 
        } 
       } else { 
        // We should always return a response 
        response.send(500, { error: 'No data read from socket' }); 
       } 
      } 
      client.destroy();    
     }); 
    }); 
} 

注意,因爲node.js的在單個線程上運行,你可以假設不會有任何情況下,響應被髮送兩次。此外,您應確保響應總是發送一次 - 在您的代碼中,如果發生套接字錯誤,或者buf.length不大於1,或者如果result.Avaiable不爲真,則會發送超時響應,但您無需等待整個(100秒)的時間發送該響應。

+0

謝謝,但這不能解決我的問題,因爲我在循環中使用此函數而在其他函數中。當function1具有'result.Avaiable == true'時,我需要停止循環。 – user3125146

+0

在這種情況下,您無法在此功能中發送回覆;而是爲函數指定另一個參數,它是一個回調函數,並將結果調用回調函數。然後在你有循環的另一個函數的代碼中,你可以用「responseSent」邏輯實現回調。 – carlosfigueira