Created
May 14, 2010 04:58
-
-
Save june29/400829 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "uri" | |
require "rubygems" | |
require "eventmachine" | |
require "em-websocket" | |
require "yajl/http_stream" | |
require "json" | |
require "pit" | |
account = Pit.get("twitter", :require => { | |
"username" => "username", | |
"password" => "password" | |
}) | |
uri = URI.parse("http://%s:%s@chirpstream.twitter.com/2b/user.json" % [account["username"], account["password"]]) | |
@channel = EM::Channel.new | |
EventMachine::run { | |
EventMachine::defer { | |
puts "server start" | |
EM::WebSocket.start(:host => "0.0.0.0", :port => 8080) do |ws| | |
ws.onopen do | |
sid = @channel.subscribe {|msg| ws.send msg} | |
puts "#{sid} connected" | |
ws.onmessage {|msg| | |
puts "<#{sid}>: #{msg}" | |
} | |
ws.onclose { | |
@channel.unsubscribe(sid) | |
puts "#{sid} closed" | |
} | |
end | |
end | |
} | |
EventMachine::defer { | |
puts "stream start" | |
Yajl::HttpStream.get(uri) do |data| | |
puts data.inspect | |
@channel.push JSON.generate(data); | |
end | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment