Skip to content

Instantly share code, notes, and snippets.

@june29
Created May 14, 2010 04:58
Show Gist options
  • Save june29/400829 to your computer and use it in GitHub Desktop.
Save june29/400829 to your computer and use it in GitHub Desktop.
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