Class: Spinderella::Client::Subscriber
- Inherits:
-
Perennial::Protocols::PureRuby::JSONTransport
- Object
- Perennial::Protocols::PureRuby::JSONTransport
- Spinderella::Client::Subscriber
- Defined in:
- lib/spinderella/client/subscriber.rb
Instance Attribute Summary collapse
-
#should_stop ⇒ Object
writeonly
Sets the attribute should_stop.
Instance Method Summary collapse
- #channels ⇒ Object
- #identify(identifier) ⇒ Object
-
#initialize(options = {}) ⇒ Subscriber
constructor
A new instance of Subscriber.
- #on_broadcast(&blk) ⇒ Object
- #on_channels(*channels, &blk) ⇒ Object
- #on_user(&blk) ⇒ Object
- #stop ⇒ Object
- #subscribe(*channels) ⇒ Object
- #unsubscribe(*channels) ⇒ Object
- #watch ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Subscriber
Returns a new instance of Subscriber.
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/spinderella/client/subscriber.rb', line 10 def initialize( = {}) @options = @host = ([:host] || "localhost").to_s @port = ([:port] || 42340).to_i @timeout = ([:timeout] || 15).to_f @read_timeout = ([:read_timeout] || 5).to_f @callbacks = {} @mutex = Mutex.new @should_stop = false end |
Instance Attribute Details
#should_stop=(value) ⇒ Object (writeonly)
Sets the attribute should_stop
8 9 10 |
# File 'lib/spinderella/client/subscriber.rb', line 8 def should_stop=(value) @should_stop = value end |
Instance Method Details
#channels ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/spinderella/client/subscriber.rb', line 36 def channels :channels action, payload = (@read_timeout) if action == "channels" Array(payload["channels"]) else [] end end |
#identify(identifier) ⇒ Object
31 32 33 34 |
# File 'lib/spinderella/client/subscriber.rb', line 31 def identify(identifier) :identify, :identifier => identifier true end |
#on_broadcast(&blk) ⇒ Object
77 78 79 80 |
# File 'lib/spinderella/client/subscriber.rb', line 77 def on_broadcast(&blk) (@callbacks[:broadcast] ||= []) << blk true end |
#on_channels(*channels, &blk) ⇒ Object
71 72 73 74 75 |
# File 'lib/spinderella/client/subscriber.rb', line 71 def on_channels(*channels, &blk) channel_callbacks = @callbacks[:channels] ||= {} (channel_callbacks[channels] ||= []) << blk true end |
#on_user(&blk) ⇒ Object
66 67 68 69 |
# File 'lib/spinderella/client/subscriber.rb', line 66 def on_user(&blk) (@callbacks[:user] ||= []) << blk true end |
#stop ⇒ Object
82 83 84 |
# File 'lib/spinderella/client/subscriber.rb', line 82 def stop @should_stop = true end |
#subscribe(*channels) ⇒ Object
21 22 23 24 |
# File 'lib/spinderella/client/subscriber.rb', line 21 def subscribe(*channels) :subscribe, :channels => Array(channels) true end |
#unsubscribe(*channels) ⇒ Object
26 27 28 29 |
# File 'lib/spinderella/client/subscriber.rb', line 26 def unsubscribe(*channels) :unsubscribe, :channels => Array(channels) true end |
#watch ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/spinderella/client/subscriber.rb', line 46 def watch loop do action, payload = (@read_timeout) # When action is nil, it means there is nothing to read yet. case action when "ping" :pong when "disconnected" @should_stop = true close when "receive_message" (payload) end break if @should_stop # In the case we didn't receive data, sleep for half a second # so as not to steal all of the available CPU. sleep 0.5 if action.nil? end end |