def listen(options = {})
raise ArgumentError, "no block provided" unless block_given?
Tinder.logger.info "Joining #{@name}…"
join
require 'json'
require 'hashie'
require 'multi_json'
require 'twitter/json_stream'
auth = connection.basic_auth_settings
options = {
:host => "streaming.#{Connection::HOST}",
:path => room_url_for('live'),
:auth => "#{auth[:username]}:#{auth[:password]}",
:timeout => 6,
:ssl => connection.options[:ssl]
}.merge(options)
Tinder.logger.info "Starting EventMachine server…"
EventMachine::run do
@stream = Twitter::JSONStream.connect(options)
Tinder.logger.info "Listening to #{@name}…"
@stream.each_item do |message|
message = Hashie::Mash.new(MultiJson.decode(message))
message = parse_message(message)
yield(message)
end
@stream.on_error do |message|
raise ListenFailed.new("got an error! #{message.inspect}!")
end
@stream.on_max_reconnects do |timeout, retries|
raise ListenFailed.new("Tried #{retries} times to connect. Got disconnected from #{@name}!")
end
raise ListenFailed.new("got disconnected from #{@name}!") if !EventMachine.reactor_running?
end
end