This repository has been archived on 2026-03-31. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
jsmag/events/listing_4.js
Ryan McGrath 5ed915c6c5 Initial
2011-02-18 04:25:06 -05:00

39 lines
992 B
JavaScript

TwitterStream.prototype.getTweets = function() {
var opts = {
host: 'stream.twitter.com',
port: 80,
path: '/1/statuses/filter.json?track=' + this.track,
method: 'POST',
headers: {
'Connection': 'keep-alive',
'Accept': '*/*',
'User-Agent': 'Example Twitter Streaming Client',
'Authorization': 'Basic ' + new Buffer(this.username + ':' + this.password).toString('base64'),
},
},
self = this;
this.connection = http.request(opts, function(response) {
response.setEncoding('utf8');
response.on('data', function(chunk) {
self.data += chunk.toString('utf8');
var index, json;
while((index = self.data.indexOf('\r\n')) > -1) {
json = self.data.slice(0, index);
self.data = self.data.slice(index + 2);
if(json.length > 0) {
try {
self.emit('tweet', JSON.parse(json));
} catch(e) {
self.emit('error', e);
}
}
}
});
});
this.connection.write('?track=' + this.track);
this.connection.end();
};