问题描述
我在我的Rails 2.1应用程序上使用faye。在测试并修复了很多东西之后 faye ruby client
无效。
I am using faye on my Rails 2.1 app. And after testing and fixing many things faye ruby client
is not working.
这是我的服务器代码。
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代码。
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>
这是ruby客户端代码。
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
服务器,JS工作正常。但Ruby客户端代码不起作用。如果我在没有 EM 的情况下编写它,它会向我显示错误事件机器未初始化
。如果我在 EM
中写它,它可以工作但是会影响ruby过程。如果我在客户端代码的末尾放置 EM.stop
,它会执行但不会发布消息。
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.
我如何解决这个问题?
推荐答案
我终于用过HTTP不是railscasts第260集中描述的Ruby Faye客户端。
I finally used HTTP not the Ruby Faye client as described in railscasts episode 260.
require 'net/http'
message = {:channel => '/faye/new_chats', :data => self.text, :ext => {:auth_token => FAYE_TOKEN}}
uri = URI.parse("http://localhost:9292/faye")
Net::HTTP.post_form(uri, :message => message.to_json)
它解决了我的问题。
注意:此解决方案仅适用于HTTP但不适用于HTTPS。如果任何人找到HTTPS的解决方案PLZ更新我。
NOTE: This solution only works with HTTP but not with HTTPS. If any one find a solution for HTTPS plz update me.
这篇关于faye ruby客户端不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!