5

我正在使用 Twitter 流 API 运行 EventMachine 进程。如果流的内容不频繁,我总是会遇到问题。

这是脚本的最小版本:

require 'rubygems'
require 'eventmachine'
require 'em-http'
require 'json'

usage = "#{$0} <user> <password> <track>"
abort usage unless user = ARGV.shift
abort usage unless password = ARGV.shift
abort usage unless keywords= ARGV.shift

def startIt(user,password,keywords)
EventMachine.run do
  http = EventMachine::HttpRequest.new("https://stream.twitter.com/1/statuses/filter.json",{:port=>443}).post(
                    :head =>{ 'Authorization' => [ user, password ] } , 
                    :body =>{"track"=>keywords},
                    :keepalive=>true,
                    :timeout=>-1)

  buffer = ""
  http.stream do |chunk|
    buffer += chunk
    while line = buffer.slice!(/.+\r?\n/)
      if line.length>5
          tweet=JSON.parse(line)
          puts Time.new.to_s+"#{tweet['user']['screen_name']}: #{tweet['text']}"
      end
    end

  end
   http.errback {
        puts Time.new.to_s+"Error: "
        puts http.error
   }
end  
    rescue => error
      puts "error rescue "+error.to_s
end

while true
    startIt user,password,keywords
end

如果我搜索“iphone”之类的关键字,一切正常如果我搜索不常用的关键字,我的信息流会在最后一条消息后大约 20 秒后快速关闭。注意:http.error 总是空的,所以当流关闭时很难理解......另一方面,非常相似的 php 版本没有关闭,所以似乎可能与 eventmachine/http-em 有问题但是没看懂是哪一个...

4

1 回答 1

6

您应该添加设置以防止连接超时。试试这个 :

http = EventMachine::HttpRequest.new(
  "https://stream.twitter.com/1/statuses/filter.json",
  :connection_timeout => 0,
  :inactivity_timeout => 0
).post(
  :head => {'Authorization' => [ user, password ] } , 
  :body => {'track' => keywords}
)

祝你好运,克里斯蒂安

于 2012-01-22T19:35:34.670 回答