Created
February 24, 2014 03:34
-
-
Save amclain/9181542 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'zeromqrb' | |
require 'timeout' | |
ctx = ZeroMQ::Context.new | |
# Subscriber | |
sub_thread = Thread.new do | |
sub_sock = ctx.socket ZMQ::SUB | |
sub_sock.setsockopt ZMQ::SUBSCRIBE, '' #Subscribe to everything. | |
sub_stat = sub_sock.bind 'tcp://127.0.0.1:5050' | |
puts "SUB Status1: #{sub_stat}" | |
sub_stat = sub_sock.bind 'tcp://127.0.0.1:5051' | |
puts "SUB Status2: #{sub_stat}" | |
begin | |
Timeout.timeout 1 do | |
loop do | |
message = '' | |
sub_sock.recv_string message | |
puts "SUB Message: #{message}" | |
end | |
end | |
rescue | |
end | |
sub_sock.close | |
end | |
# Publisher | |
pub_sock = ctx.socket ZMQ::PUB | |
pub_stat = pub_sock.connect 'tcp://127.0.0.1:5050' | |
puts "PUB1 Status: #{pub_stat}" | |
# Publisher 2 | |
pub2_sock = ctx.socket ZMQ::PUB | |
pub2_stat = pub2_sock.connect 'tcp://127.0.0.1:5051' | |
puts "PUB2 Status: #{pub2_stat}" | |
sleep 0.1 # Let the subscriber spin up. | |
pub_sock.send_string 'Hello1' | |
sleep 0.1 | |
pub2_sock.send_string 'Hello2' | |
pub_sock.close | |
pub2_sock.close | |
# Cleanup | |
sub_thread.join | |
ctx.terminate |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment