使用tweetstream和超时

时间:2013-04-27 00:29:13

标签: ruby tweetstream

Ruby版本:2.0.0-p0 Mac - Mountain Lion

以下是我的代码(tw_stream_track.rb):

require 'tweetstream'

TweetStream.configure do |config|
  config.consumer_key       = 'xxxxxxxxxxxxxxx'
  config.consumer_secret    = 'xxxxxxxxxxxxxxx'
  config.oauth_token        = 'xxxxxxxxxxxxxxx-xxxxxxxxxxxxxxx'
  config.oauth_token_secret = 'xxxxxxxxxxxxxxx'
  config.auth_method        = :oauth
end

client = TweetStream::Client.new

client.on_error do |message|
  puts message
end

client.track('apple', 'microsoft', 'samsung') do |status|
  puts "#{status.text}"
end

当我从终端运行时:

ruby tw_stream_track.rb

我得到以下内容:

/Users/mine/.rvm/gems/ruby-2.0.0-p0/gems/tweetstream-2.5.0/lib/tweetstream/client.rb:436:in `block in connect': Failed to reconnect after 11 tries. (TweetStream::ReconnectError)
    from /Users/mine/.rvm/gems/ruby-2.0.0-p0/gems/em-twitter-0.2.2/lib/em-twitter/connection.rb:296:in `call'
    from /Users/mine/.rvm/gems/ruby-2.0.0-p0/gems/em-twitter-0.2.2/lib/em-twitter/connection.rb:296:in `invoke_callback'
    from /Users/mine/.rvm/gems/ruby-2.0.0-p0/gems/em-twitter-0.2.2/lib/em-twitter/connection.rb:268:in `rescue in schedule_reconnect'
    from /Users/mine/.rvm/gems/ruby-2.0.0-p0/gems/em-twitter-0.2.2/lib/em-twitter/connection.rb:263:in `schedule_reconnect'
    from /Users/mine/.rvm/gems/ruby-2.0.0-p0/gems/em-twitter-0.2.2/lib/em-twitter/connection.rb:93:in `unbind'
    from /Users/mine/.rvm/gems/ruby-2.0.0-p0/gems/eventmachine-1.0.3/lib/eventmachine.rb:1440:in `event_callback'
    from /Users/mine/.rvm/gems/ruby-2.0.0-p0/gems/eventmachine-1.0.3/lib/eventmachine.rb:187:in `run_machine'
    from /Users/mine/.rvm/gems/ruby-2.0.0-p0/gems/eventmachine-1.0.3/lib/eventmachine.rb:187:in `run'
    from /Users/mine/.rvm/gems/ruby-2.0.0-p0/gems/tweetstream-2.5.0/lib/tweetstream/client.rb:388:in `start'
    from /Users/mine/.rvm/gems/ruby-2.0.0-p0/gems/tweetstream-2.5.0/lib/tweetstream/client.rb:131:in `filter'
    from /Users/mine/.rvm/gems/ruby-2.0.0-p0/gems/tweetstream-2.5.0/lib/tweetstream/client.rb:98:in `track'

这是我第一次使用tweetstream,从文档来看,似乎这就是我应该做的事情。我错过了什么?

2 个答案:

答案 0 :(得分:1)

我听说TweetStream在ruby 2.0.0上遇到了一些问题,你是否尝试过使用ruby 1.9.3?

<强>更新

我刚试了一下,你的代码在ruby 1.9.3(与rvm一起安装)上运行良好。 我唯一的一点是,流似乎在一段时间后停止检索新消息。

答案 1 :(得分:0)

在我的情况下出现此错误是因为我在2个rails应用程序实例(在登台和生产服务器上)+ 1个本地(开发)实例中使用相同的密钥。

第3个实例无法连接到推文流。 It is cause because of the twitter limitations

所以我通过为每个Rails应用程序实例创建专用的twitter应用程序(带有单独的密钥)来解决这个问题。