我在Faye机架脚本中有这段代码:
faye_server = Faye::RackAdapter.new(:mount => '/faye', :timeout => 45)
faye_server.add_extension(ServerAuth.new)
server_uri = URI.parse(BacklinkHealth::FAYE_SERVER)
faye_server.on(:subscribe) do |client_id, channel|
puts "subscribed #{channel}"
if channel.starts_with?('/pagination')
website_id = channel.split('/')[2]
page = channel.split('/')[3]
app = ActionDispatch::Integration::Session.new(Rails.application)
app.get app.pagination_website_backlinks_path(website_id, :page => page)
message = {:channel => channel, :data => app.response.body, :ext => {:auth_token => BacklinkHealth::FAYE_TOKEN}}
puts 'started HTTP post'
Net::HTTP.post_form(server_uri, :message => message.to_json)
puts 'finished HTTP post'
end
end
问题在于执行是“启动HTTP post”,然后在客户端javascript上注册消息需要一分多钟。从未打印出“完成HTTP帖子”的消息:(我不明白发生了什么。
如果我尝试从Rails控制台执行相同的HTTP帖子,它会在一瞬间完成,尽管它确实发生了一两次花了一分钟。
有什么想法吗?
答案 0 :(得分:0)
使用它会好得多:
require 'eventmachine'
EM.run {
client = Faye::Client.new(BacklinkHealth::FAYE_SERVER)
client.publish(channel, 'pagination' => pagination)
}
比Http请求...我无法使用身份验证(但是),但我会。