Created
February 20, 2014 19:06
-
-
Save amclain/9120890 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 'ffi-rzmq' | |
require 'timeout' | |
# socket_path = 'tcp://127.0.0.1:5050' | |
# socket_path = 'ipc://zmq_pub_sub.ipc' | |
socket_path = 'epgm://eth0;239.0.0.1:5050' | |
ctx = ZMQ::Context.create | |
# 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 socket_path | |
# sub_stat = sub_sock.connect socket_path | |
puts "SUB Bind: #{sub_stat}" | |
message = '' | |
begin | |
Timeout.timeout 5 do | |
loop do | |
sub_sock.recv_string message | |
puts "SUB Message: #{message}" | |
end | |
end | |
rescue | |
puts "Done" | |
end | |
sub_sock.close | |
end | |
# Publisher | |
pub_sock = ctx.socket ZMQ::PUB | |
# pub_stat = pub_sock.connect socket_path | |
pub_stat = pub_sock.bind socket_path | |
puts "PUB Bind: #{pub_stat}" | |
sleep 0.1 # Let the subscriber spin up. | |
# pub_sock.send_string 'Hello' | |
(1..5).each do |i| | |
pub_sock.send_string "Message #{i}" | |
sleep 0.5 | |
end | |
pub_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