Skip to content

Instantly share code, notes, and snippets.

@jtoy
Created February 22, 2011 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtoy/839018 to your computer and use it in GitHub Desktop.
Save jtoy/839018 to your computer and use it in GitHub Desktop.
no reconnect?
class TwitterStream < EventMachine::Connection
def self.connect
EventMachine::HttpRequest.new("http://stream.twitter.com/1/statuses/filter.json").post({
:body =>{'track' => 'hello,world,how,are,you'},
:head => { 'Authorization' => [ 'user','pass' ] }
})
end
def on_error &block
puts "reconnecting"
reconnect
end
end
EventMachine.run do
stream = TwitterStream.connect
EventMachine.add_periodic_timer(1){ puts "." } if $debug
#EventMachine.add_periodic_timer(3600){ $file.close;$file = open_file }
stream.callback {
unless http.response_header.status == 200
puts "Call failed with response code #{http.response_header.status}"
else
puts 'wtf?'
puts http.response_header
end
}
stream.stream do |data|
(@buffer ||= BufferedTokenizer.new("\r\n")).extract(data).each do |line|
puts line
end
end
trap('TERM') {
EventMachine.stop if EventMachine.reactor_running?
}
end
puts "The event loop has ended"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment