Created
May 28, 2012 04:30
-
-
Save qmx/2817228 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 'rubygems' | |
require 'ffi-rzmq' | |
# Prepare our context and socket | |
context = ZMQ::Context.new(1) | |
t2=Thread.new do | |
begin | |
publisher = context.socket(ZMQ::PUSH) | |
publisher.connect("tcp://localhost:5558") | |
(1..10).each {|n| | |
publisher.send_string "MARIO", ZMQ::SNDMORE | |
publisher.send_string "It's a me! #{n}" | |
sleep rand(5)/10.0 | |
} | |
rescue | |
puts $! | |
puts $@ | |
end | |
end | |
t1=Thread.new do | |
publisher = context.socket(ZMQ::PUSH) | |
publisher.connect("tcp://localhost:5558") | |
(1..10).each {|n| | |
publisher.send_string "PEACH", ZMQ::SNDMORE | |
publisher.send_string "Peachy!" | |
sleep rand(5)/10.0 | |
} | |
end | |
t1.join | |
t2.join | |
publisher = context.socket(ZMQ::PUSH) | |
publisher.connect("tcp://localhost:5558") | |
publisher.send_string "DIE" |
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 'rubygems' | |
require 'ffi-rzmq' | |
# Prepare our context and socket | |
context = ZMQ::Context.new(1) | |
subscriber = context.socket(ZMQ::SUB) | |
subscriber.connect("tcp://localhost:5559") | |
subscriber.setsockopt(ZMQ::SUBSCRIBE,"MARIO") | |
subscriber.setsockopt(ZMQ::SUBSCRIBE,"DIE") | |
trap("SIGINT") {exit!} | |
loop do | |
subscriber.recv_string(topic='') | |
subscriber.recv_string(body='') if subscriber.more_parts? | |
puts "Got topic #{topic} body #{body}" | |
exit if topic=="DIE" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment