我正在使用FAYE和rails应用程序 令我非常恼火的唯一一件事是我不能 在收到的邮件上创建或更改模型。
好像它应该像https://github.com/jamesotron/faye-rails 但它有点麻烦。
有没有办法通过扩展程序更新模型? 也许是这样的:
require 'faye'
require './app/models/message.rb'
Faye::WebSocket.load_adapter('thin')
require File.expand_path('../config/initializers/faye_token.rb', __FILE__)
class MsgMonitor
def incoming(message, callback)
Message.create(:name=>message.to_s)
callback.call(message)
end
end
faye_server = Faye::RackAdapter.new(:mount => '/faye', :timeout => 45)
faye_server.add_extension(ServerAuth.new)
faye_server.add_extension(MsgMonitor.new)
run faye_server
但它会出错。因此,它应该以某种方式加载整个rails环境(实际上是需要的)。
任何帮助都将受到高度赞赏....
PS尝试使用Google Group文章http://groups.google.com/group/faye-users/browse_thread/thread/620ee6440422687a?pli=1加入渠道 但仍然无法让它工作。它发布但没有通过订阅回复。
答案 0 :(得分:0)
为我工作的是让faye服务器只负责传递和验证消息。如果成功,我将它们以resque的方式保存到redis中,以便完成处理。
好处是长时间运行的任务不会减慢您的连接速度,如果您需要进一步处理,您可以随时添加更多工作人员。
在我的第一个扩展程序的传入方法的顶部,我创建了一个新的em-redis连接
$redis ||= EM::Hiredis.connect @options[:redis]
然后,如果它通过我的测试,我会以resque使用的格式将其存储在redis中。
m = {'class' => "Message", 'args' => ['handle_message', {name: message.to_s}.to_json]}
$redis.rpush("resque:queue:faye", m.to_json )
$redis.sadd("resque:queues","faye")
用于将我的rails应用程序传输给用户我在脚本/ faye_worker中使用以下代码
https://github.com/SponsorPay/em-resque#in-a-rails-3-app-as-a-gem
我用EM.synchrony包装上面的内容,以便我可以在worker
之前初始化faye从模型中发送消息只需将其发送到异步resque队列。