Class: Spinderella::Client::Subscriber

Inherits:
Perennial::Protocols::PureRuby::JSONTransport
  • Object
show all
Defined in:
lib/spinderella/client/subscriber.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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 = {})
  @options       = options
  @host          = (options[:host] || "localhost").to_s
  @port          = (options[:port] || 42340).to_i
  @timeout       = (options[:timeout] || 15).to_f
  @read_timeout  = (options[: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

Parameters:

  • value

    the value to set the attribute should_stop to.



8
9
10
# File 'lib/spinderella/client/subscriber.rb', line 8

def should_stop=(value)
  @should_stop = value
end

Instance Method Details

#channelsObject



36
37
38
39
40
41
42
43
44
# File 'lib/spinderella/client/subscriber.rb', line 36

def channels
  write_message :channels
  action, payload = read_message(@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)
  write_message :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

#stopObject



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)
  write_message :subscribe, :channels => Array(channels)
  true
end

#unsubscribe(*channels) ⇒ Object



26
27
28
29
# File 'lib/spinderella/client/subscriber.rb', line 26

def unsubscribe(*channels)
  write_message :unsubscribe, :channels => Array(channels)
  true
end

#watchObject



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_message(@read_timeout)
    # When action is nil, it means there is nothing to read yet.
    case action
      when "ping"
        write_message :pong
      when "disconnected"
        @should_stop = true
        close
      when "receive_message"
        process_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