faye ruby 客户端无法工作
我在 Rails 2.1 应用程序上使用 faye。经过测试和修复许多问题后,faye ruby 客户端
无法正常工作。
这是我的服务器代码。
require 'faye'
server = Faye::RackAdapter.new(:mount => '/faye', :timeout => 45)
EM.run {
thin = Rack::Handler.get('thin')
thin.run(server, :Port => 9292)
server.bind(:subscribe) do |client_id, channel|
puts "[ SUBSCRIBE] #{client_id} -> #{channel}"
end
server.bind(:unsubscribe) do |client_id, channel|
puts "[UNSUBSCRIBE] #{client_id} -> #{channel}"
end
server.bind(:disconnect) do |client_id|
puts "[ DISCONNECT] #{client_id}"
end
}
这是我的客户端 JS 代码。
<script type="text/javascript">
var client = new Faye.Client('http://localhost:9292/faye');
client.subscribe("/faye/new_chats", function(data) {
console.log(data);
});
</script>
这是 ruby 客户端代码。
EM.run do
client = Faye::Client.new('http://localhost:9292/faye')
publication = client.publish("/faye/new_chats", {
"user" => "ruby-logger",
"message" => "Got your message!"
})
publication.callback do
puts "[PUBLISH SUCCEEDED]"
end
publication.errback do |error|
puts "[PUBLISH FAILED] #{error.inspect}"
end
end
服务器,JS工作正常。但 Ruby 客户端代码无法正常工作。如果我在没有EM的情况下编写它,它会显示事件机器未初始化
的错误。如果我用 EM
编写它,它可以工作,但会拖累 ruby 进程。如果我将 EM.stop
放在客户端代码的末尾,它将执行但不发布消息。
我该如何解决这个问题?
I am using faye on my Rails 2.1 app. And after testing and fixing many things faye ruby client
is not working.
This is my server code.
require 'faye'
server = Faye::RackAdapter.new(:mount => '/faye', :timeout => 45)
EM.run {
thin = Rack::Handler.get('thin')
thin.run(server, :Port => 9292)
server.bind(:subscribe) do |client_id, channel|
puts "[ SUBSCRIBE] #{client_id} -> #{channel}"
end
server.bind(:unsubscribe) do |client_id, channel|
puts "[UNSUBSCRIBE] #{client_id} -> #{channel}"
end
server.bind(:disconnect) do |client_id|
puts "[ DISCONNECT] #{client_id}"
end
}
This is my client side JS code.
<script type="text/javascript">
var client = new Faye.Client('http://localhost:9292/faye');
client.subscribe("/faye/new_chats", function(data) {
console.log(data);
});
</script>
This is ruby client code.
EM.run do
client = Faye::Client.new('http://localhost:9292/faye')
publication = client.publish("/faye/new_chats", {
"user" => "ruby-logger",
"message" => "Got your message!"
})
publication.callback do
puts "[PUBLISH SUCCEEDED]"
end
publication.errback do |error|
puts "[PUBLISH FAILED] #{error.inspect}"
end
end
Server, JS is working fine. But Ruby client code is not working. If i write it without EM it shows me the error that Event Machine not initialized
. If i write it in EM
it works but haults the ruby process. If i put EM.stop
at the end of client code, it executes but do not publish the message.
How can i solve this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你就快到了...你只需要在回调中停止 EM 事件循环,如下所示:
You were almost there... You just needed to stop the EM event loop in your callbacks, like this:
我最终使用了 HTTP,而不是 Railscasts 第 260 集中描述的 Ruby Faye 客户端。
它解决了我的问题。
注意:此解决方案仅适用于 HTTP,不适用于 HTTPS。如果有人找到 HTTPS 的解决方案,请更新我。
I finally used HTTP not the Ruby Faye client as described in railscasts episode 260.
It solves my problem.
NOTE: This solution only works with HTTP but not with HTTPS. If any one find a solution for HTTPS plz update me.