请求-响应模式不适用于 em-zeromq

发布于 2025-01-04 16:01:24 字数 1444 浏览 0 评论 0原文

我正在尝试使用 em-zeromq gem 实现请求-响应模式,但我无法让响应套接字将消息发送回其处理程序中的请求套接字。我编写了一些非常简单的代码来测试它:

em_req.rb

require 'em-zeromq'

client_id = ARGV[0] ? ARGV[0].to_i : 1
message = ARGV[1] || "Foo"

Thread.abort_on_exception = true

class ReqHandler
  attr_reader :received

  def on_readable(socket, messages)
    messages.each do |m|
      puts "Received message from server: #{m.copy_out_string}"
    end
  end
end

trap('INT') do
  EM.stop
end

ctx = EM::ZeroMQ::Context.new(1)

EM.run do
  conn = ctx.connect(ZMQ::REQ, 'tcp://127.0.0.1:9000', ReqHandler.new, identity: "client#{client_id}")
  conn.socket.send_string(message)
end

em_rep.rb

require 'em-zeromq'

Thread.abort_on_exception = true

class ResponseHandler
  attr_reader :received

  def on_readable(socket, messages)
    message = messages.first.copy_out_string
    puts "Received message from client: #{message}"

    socket.send_msg("re: #{message}")
  end
end

trap('INT') do
  EM.stop
end

ctx = EM::ZeroMQ::Context.new(1)

EM.run do
  socket = ctx.bind(ZMQ::REP, 'tcp://127.0.0.1:9000', ResponseHandler.new)
end

我已经使用推拉模式编写了类似的代码并使其工作,但对于请求响应,我得到的只是打印“已接收消息”的响应代码from client1: Foo”,但回复从未到达请求代码。我怀疑这与写入响应代码处理程序中的套接字有关,因为当我使用请求路由器模式时也会发生同样的事情。它唯一起作用的时候是当我从服务器发送消息而没有先从客户端发送消息时(使用推拉)。

关于可能导致这种情况的原因有什么想法吗? gem 的作者不再维护它,但我想无论如何我都会发布这个问题,希望其他有类似经验的开发人员看到这个问题。

我在 Ruby 1.9.2p290 上使用 em-zeromq 0.2.2。

I am trying to implement a request-response pattern using the em-zeromq gem, but I can't get the response socket to send a message back to the request socket in its handler. I have written some very simple code to test it:

em_req.rb

require 'em-zeromq'

client_id = ARGV[0] ? ARGV[0].to_i : 1
message = ARGV[1] || "Foo"

Thread.abort_on_exception = true

class ReqHandler
  attr_reader :received

  def on_readable(socket, messages)
    messages.each do |m|
      puts "Received message from server: #{m.copy_out_string}"
    end
  end
end

trap('INT') do
  EM.stop
end

ctx = EM::ZeroMQ::Context.new(1)

EM.run do
  conn = ctx.connect(ZMQ::REQ, 'tcp://127.0.0.1:9000', ReqHandler.new, identity: "client#{client_id}")
  conn.socket.send_string(message)
end

em_rep.rb

require 'em-zeromq'

Thread.abort_on_exception = true

class ResponseHandler
  attr_reader :received

  def on_readable(socket, messages)
    message = messages.first.copy_out_string
    puts "Received message from client: #{message}"

    socket.send_msg("re: #{message}")
  end
end

trap('INT') do
  EM.stop
end

ctx = EM::ZeroMQ::Context.new(1)

EM.run do
  socket = ctx.bind(ZMQ::REP, 'tcp://127.0.0.1:9000', ResponseHandler.new)
end

I have written similar code using the push-pull pattern and got that to work, but for request-response all I get is the response code printing "Received message from client1: Foo" but the reply never reaches the request code. I suspect it has to do with writing to the socket in the response code's handler, because the same thing happens when I use a request-router pattern. The only time it works is when I send a message from the server without sending a message from the client first (using push-pull).

Any ideas about what might be causing this? The author of the gem isn't maintaining it anymore, but I thought I would post this issue anyway in the hopes of other developers with similar experiences seeing this.

I am using em-zeromq 0.2.2 on Ruby 1.9.2p290.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

梦行七里 2025-01-11 16:01:24

我在 master 分支中提交了一个修复程序,应该可以解决您的问题,您可以尝试一下吗?

您可以使用bundler轻松测试它:
在您的应用程序文件夹中创建一个名为 Gemfile 的文件:

source :rubygems
gem 'em-zeromq', :git => "git://github.com/andrewvc/em-zeromq.git"

并将其添加到您的 ruby​​ 文件之上:

require 'rubygems'
require 'bundler/setup'

最后在应用程序文件夹中运行此文件($ 是您的提示符):

$ bundle

现在您可以执行您的 ruby​​ 文件,它们将使用来自 github 的最新代码

编辑:我是 em-zeromq gem 的新维护者。

I commmited a fix in the master branch which should solve your problem, can you give it a try ?

You can use bundler to easily test it:
Create a file called Gemfile in your application folder:

source :rubygems
gem 'em-zeromq', :git => "git://github.com/andrewvc/em-zeromq.git"

And add this on top of your ruby files:

require 'rubygems'
require 'bundler/setup'

And last run this in the application folder ($ is your prompt):

$ bundle

Now you can execute your ruby files they will use the latest code from github

Edit: I am the new maintainer for the em-zeromq gem.

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