4

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,从文档来看,这似乎是我应该做的。我错过了什么?

4

2 回答 2

1

我听说 TweetStream 对 ruby​​ 2.0.0 有一些问题,你用 ruby​​ 1.9.3 试过了吗?

更新

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

于 2013-06-11T09:27:39.240 回答
0

就我而言,出现此错误是因为我在 2 个 rails 应用程序实例(在登台和生产服务器上)+ 1 个本地(开发)实例中使用了相同的键。

第三个实例无法连接到推文流。这是因为twitter限制的原因

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

于 2015-01-09T18:14:53.280 回答