2013-02-23 69 views
3

當使用https連接到使用基本身份驗證的twitter流api時,出現socket hang up錯誤。我認爲在進行身份驗證時發生錯誤。當連接到twitter api時,Node.js套接字掛起錯誤

var https = require("https"); 
var options = { 
     host: 'stream.twitter.com', 
     path: '/1.1/statuses/filter.json?track=bieber', 
     method: 'GET', 
     headers: { 
       "Authorization": "Basic " + new Buffer('UserName' + ":" + 'Password').toString("base64") 
     } 
}; //end of options 


var request = https.request(options, function(response){ 
      var body = ''; 
      console.log('STATUS: ' + response.statusCode); 
      console.log('HEADERS: ' + JSON.stringify(response.headers)); 

      response.on("data", function(chunk){ 
        var tweet = JSON.parse(chunk); 
        console.log("Tweet " + tweet.text); 
      }); // end of data 

      response.on("end", function(){ 
       console.log("Disconnected!!"); 
      }); // end of end 

      response.on("error", function(){ 
       console.log("error occured"); 
      }); // end of error 
}); // end of request 

request.on("error",function(error){ 
     console.log("Error: " + error.message); 
       }); // end of error 

當我嘗試訪問此網址時,它工作正常。

https://username:[email protected]/1.1/statuses/filter.json?track=twitter 
+1

此錯誤已被列入0.8.20的NodeJS(你可以在看到https://raw.github.com/joyent/node/v0。 8.20 /更新日誌)。它發生在套接字連接結束時。我使用與以前的Node版本完美配合的代碼嘗試了這個錯誤。 – sgmonda 2013-02-24 11:37:49

回答

5

變化https.request(options, function(response){ }https.get(options, function(response){ }感謝Darrenlooby在IRC的NodeJS指點一下。

+0

有誰知道原因? – LeeGee 2016-11-10 09:59:20