Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 29th, 2012  |  syntax: None  |  size: 0.51 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. # usage: ruby sub.rb
  2. #
  3. # Connects a SUB socket to tcp://*:5555.
  4. # Subscribes to rubyonrails and ruby-lang.
  5.  
  6. require 'rubygems'
  7. require 'zmq'
  8.  
  9. context = ZMQ::Context.new
  10. chans   = %w(rubyonrails ruby-lang ping)
  11. sub     = context.socket ZMQ::SUB
  12.  
  13. if id = ARGV[0]
  14.   sub.setsockopt ZMQ::IDENTITY, id
  15.   puts "Identified as #{id}"
  16. end
  17.  
  18. sub.connect 'tcp://127.0.0.1:5555'
  19. chans.each { |ch| sub.setsockopt ZMQ::SUBSCRIBE, ch }
  20.  
  21. while line = sub.recv
  22.   chan, user, msg = line.split ' ', 3
  23.   puts "##{chan} [#{user}]: #{msg}"
  24. end