저는 ZeroMQ를 EventMachine과 함께 사용하는 방법을 배우고 있습니다. 테스트를 위해 XREQ 및 XREP 소켓을 사용하는 루비 (에코 클라이언트 서버)에 작은 프로그램을 작성했습니다. 클라이언트 응용 프로그램이 서버에 메시지를 보내고 (연속 번호) 응답에서 다시 가져옵니다. 전송 간격은 0.1 초입니다. 모든 것이 일정한 순간까지 작동합니다. 현재 숫자가 약 400에 도달하면 서버가 멈추고 더 이상 클라이언트에 응답하지 않습니다. 나는 이것을 여러 대의 컴퓨터에서 테스트했지만 여전히 이상한 문제가 있습니다. ZeroMQ 구동 서버가 잠시 후 응답을하지 않습니다.
코드
은 매우 간단합니다 :server.rb]로
require 'rubygems'
require 'bundler/setup'
require 'em-zeromq'
Thread.abort_on_exception = true
ADDRESS = 'tcp://127.0.0.1:2091'
class EMServerHandler
attr_reader :received
def on_readable(socket, messages)
client_identity = messages.shift.copy_out_string #getting client identity from the 1st part of the message
messages.shift #skip the delimeter
messages.each do |m|
msg = m.copy_out_string
puts "server received from #{client_identity}: " + msg
socket.send_msg("#{client_identity}",'',"#{msg}") #echo message back to the client
end
end
end
trap('INT') do
EM::stop()
end
puts "Program started (with zmq #{ZMQ::Util.version.join('.')})."
EM.run do
EventMachine.epoll
ctx = EM::ZeroMQ::Context.new(1)
server = ctx.bind(ZMQ::XREP, ADDRESS, EMServerHandler.new, {:identity => "server"})
end
client.rb
require 'rubygems'
require 'bundler/setup'
require 'em-zeromq'
Thread.abort_on_exception = true
ADDRESS = 'tcp://127.0.0.1:2091'
class EMClientHandler
attr_reader :received
def on_readable(socket, messages)
messages.shift #skip the delimeter
messages.each do |m|
puts "client recieved: " + m.copy_out_string
end
end
end
trap('INT') do
EM::stop()
end
puts "Program started (with zmq #{ZMQ::Util.version.join('.')})."
EM.run do
EventMachine.epoll
ctx = EM::ZeroMQ::Context.new(1)
puts "client"
puts "enter client name >> "
identity = gets.strip
client = ctx.connect(ZMQ::XREQ, ADDRESS, EMClientHandler.new, {:identity => identity})
client.send_msg('', "hello from client #{identity}")
count = 0
EM::PeriodicTimer.new(0.1) do
client.send_msg('', "#{count += 1}")
end
end
나에게 그 이유를 알아내는 데 도움이 바랍니다.