faye ruby​​ 客户端无法工作

发布于 2024-12-22 01:28:48 字数 1538 浏览 3 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

耶耶耶 2024-12-29 01:28:48

你就快到了...你只需要在回调中停止 EM 事件循环,如下所示:

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]"
    EM.stop_event_loop
  end
  publication.errback do |error|
    puts "[PUBLISH FAILED] #{error.inspect}"
    EM.stop_event_loop
  end
end

You were almost there... You just needed to stop the EM event loop in your callbacks, like this:

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]"
    EM.stop_event_loop
  end
  publication.errback do |error|
    puts "[PUBLISH FAILED] #{error.inspect}"
    EM.stop_event_loop
  end
end
煮酒 2024-12-29 01:28:48

我最终使用了 HTTP,而不是 Railscasts 第 260 集中描述的 Ruby Faye 客户端。

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 的解决方案,请更新我。

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)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文