Initial
This commit is contained in:
commit
5ed915c6c5
8 changed files with 329 additions and 0 deletions
8
events/listing_1.js
Normal file
8
events/listing_1.js
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
var x = 1;
|
||||
var foo = function(callbackfn) {
|
||||
return callbackfn(x * 2);
|
||||
};
|
||||
|
||||
foo(function(x) {
|
||||
console.log(x);
|
||||
});
|
||||
20
events/listing_2.js
Normal file
20
events/listing_2.js
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
var events = require('events'),
|
||||
util = require('util');
|
||||
|
||||
var Foo = function(initial_no) { this.count = initial_no; };
|
||||
|
||||
Foo.prototype = new events.EventEmitter;
|
||||
|
||||
Foo.prototype.increment = function() {
|
||||
var self = this;
|
||||
setInterval(function() {
|
||||
if(self.count % 2 === 0) self.emit('even');
|
||||
self.count++;
|
||||
}, 300);
|
||||
};
|
||||
|
||||
var lol = new Foo(1);
|
||||
|
||||
lol.on('even', function() {
|
||||
util.puts('Number is even! :: ' + this.count);
|
||||
}).increment();
|
||||
13
events/listing_3.js
Normal file
13
events/listing_3.js
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
var util = require('util'),
|
||||
http = require('http'),
|
||||
events = require('events');
|
||||
|
||||
var TwitterStream = function(opts) {
|
||||
this.username = opts.username;
|
||||
this.password = opts.password;
|
||||
this.track = opts.track;
|
||||
this.data = '';
|
||||
};
|
||||
|
||||
TwitterStream.prototype = new events.EventEmitter;
|
||||
module.exports = TwitterStream;
|
||||
39
events/listing_4.js
Normal file
39
events/listing_4.js
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
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();
|
||||
};
|
||||
18
events/listing_5.js
Normal file
18
events/listing_5.js
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
var TwitterStream = require('./twitterstream'),
|
||||
util = require('util');
|
||||
|
||||
var twitter = new TwitterStream({
|
||||
username: 'username',
|
||||
password: 'password',
|
||||
track: 'JavaScript',
|
||||
});
|
||||
|
||||
twitter.on('tweet', function(tweet) {
|
||||
util.puts(util.inspect(tweet));
|
||||
})
|
||||
|
||||
twitter.on('error', function(e) {
|
||||
util.puts(e);
|
||||
})
|
||||
|
||||
twitter.getTweets();
|
||||
54
events/twitterstream.js
Normal file
54
events/twitterstream.js
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
var util = require('util'),
|
||||
http = require('http'),
|
||||
events = require('events');
|
||||
|
||||
var TwitterStream = function(opts) {
|
||||
this.username = opts.username;
|
||||
this.password = opts.password;
|
||||
this.track = opts.track;
|
||||
this.data = '';
|
||||
};
|
||||
|
||||
TwitterStream.prototype = new events.EventEmitter;
|
||||
|
||||
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();
|
||||
};
|
||||
|
||||
module.exports = TwitterStream;
|
||||
Reference in a new issue