Class: SPSSub
- Inherits:
-
Object
- Object
- SPSSub
- Defined in:
- lib/sps-sub.rb
Instance Method Summary collapse
- #em_connect(topic, &blk) ⇒ Object
-
#initialize(hosts: [], port: '59000', host: nil, address: nil, callback: nil, log: nil) ⇒ SPSSub
constructor
A new instance of SPSSub.
- #notice(s) ⇒ Object
-
#onmessage(msg) ⇒ Object
This method is called when a new message is received.
-
#ontopic(topic, msg) ⇒ Object
Same as onmessage but includes the topic as well as the msg.
- #subscribe(topic: '#', &blk) ⇒ Object
Constructor Details
#initialize(hosts: [], port: '59000', host: nil, address: nil, callback: nil, log: nil) ⇒ SPSSub
Returns a new instance of SPSSub.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/sps-sub.rb', line 10 def initialize(hosts: [], port: '59000', host: nil, address: nil, callback: nil, log: nil ) log.info 'SPSSub/initialize: active' if log if host.nil? and address.nil? and hosts.any? then hostx, portx = hosts.first.split(':',2) portx ||= port @host, @port = hostx, portx else @host = host || address || 'localhost' @port = port.to_s end @callback = callback @retry_interval = 1 @hosts = hosts @log = log # Trap ^C Signal.trap("INT") { puts ' ... Bye' @status = :quit exit } # Trap `Kill ` Signal.trap("TERM") { @status = :quit exit } end |
Instance Method Details
#em_connect(topic, &blk) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/sps-sub.rb', line 59 def em_connect(topic, &blk) client = self host, port, log = @host, @port, @log EM.run do address = host + ':' + port @ws = ws = WebSocket::EventMachine::Client.connect(:uri => 'ws://' + address) ws.onopen do status_msg = "Connected" puts status_msg log.info 'SPSSub/em_connect: onopen: ' + status_msg if log end ws. do |fqm, type| topic, msg = fqm.split(/:\s/,2) EM.defer do if block_given? then blk.call(msg.to_s, topic.to_s) elsif @callback @callback.ontopic(topic, msg) else msg ontopic topic, msg end end end ws.onclose do status_msg = "Disconnected" log.info 'SPSSub/em_connect: onclose: ' + status_msg if log return if @status == :quit if @hosts.any? then hostx, portx = @hosts.rotate!.first.split(':',2) portx ||= @port @host, @port = hostx, portx client.em_connect topic else @retry_interval *= 2 sleep @retry_interval @retry_interval = 1 if @retry_interval > 30 status_msg = "retrying to connect to #{@host}:#{@port}... " puts status_msg log.info 'SPSSub/em_connect: onclose' + status_msg if log client.em_connect topic end end ws.onerror do |error| status_msg = "Error occured: #{error}" puts status_msg log.info 'SPSSub/em_connect: onerror: ' + status_msg if log end EventMachine.next_tick do ws.send 'subscribe to topic: ' + topic end end end |
#notice(s) ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/sps-sub.rb', line 44 def notice(s) EventMachine.next_tick do @ws.send s end end |
#onmessage(msg) ⇒ Object
This method is called when a new message is received
136 137 138 |
# File 'lib/sps-sub.rb', line 136 def (msg) puts "Received message: #{msg}" end |
#ontopic(topic, msg) ⇒ Object
Same as onmessage but includes the topic as well as the msg
142 143 144 |
# File 'lib/sps-sub.rb', line 142 def ontopic(topic, msg) end |
#subscribe(topic: '#', &blk) ⇒ Object
52 53 54 55 56 57 |
# File 'lib/sps-sub.rb', line 52 def subscribe(topic: '#', &blk) @t = Time.now em_connect(topic, &blk) end |