我正在使用Twitter库的流功能。当用户注册时,它开始流式传输他们的推文:
class UserStream
def begin_stream()
Thread.new do
# Setup the client
@client = Twitter::Streaming::Client.new do |config|
config.consumer_key = ConsumerKey
config.consumer_secret = ConsumerSecret
config.access_token = @access_token
config.access_token_secret = @access_token_secret
end
@client.user do |object|
puts object
yield object
end
end
end
然后将UserStream对象添加到数组中,以使流保持活动状态。 begin_stream
函数由另一个函数“产生”,以接收事件:
user_stream = UserStream.new(user_id, access_token, access_token_secret)
@user_streams << user_stream
Thread.new do
user_stream.begin_stream do |message|
# Respond to event
end
end
但是稍后当从阵列中删除UserStream时,事件会继续触发。我将它从数组中删除的意图是将它设为零,因为它没有引用,并停止流。 Twitter Streaming Client似乎没有办法停止流。
编辑:删除流的代码:
def remove_stream(user_id)
index = @user_streams.index do |user_stream|
user_stream.user_id == user_id
end
if index
@user_streams.delete_at(index)
end
end