Class: JPC::Dispatcher

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/jpc/dispatcher.rb

Instance Method Summary collapse

Methods included from Helpers

#make_error, #make_message, #make_response, #make_result

Instance Method Details

#cast(channel, payload) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/jpc/dispatcher.rb', line 22

def cast(channel, payload)
  message = make_message(channel: channel, payload: payload)

  sent = 0

  channels[channel.to_sym] && channels[channel.to_sym].each do |ws|
    ws.send(message)
    sent += 1
  end

  { channel: channel, sent: sent }
end

#subscribe(ws, channel) ⇒ Object



5
6
7
8
9
10
# File 'lib/jpc/dispatcher.rb', line 5

def subscribe(ws, channel)
  channels[channel.to_sym] ||= []
  channels[channel.to_sym].push(ws) unless channels[channel.to_sym].include?(ws)

  { channel: channel, status: 'subscribed' }
end

#unsubscribe(ws, channel) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/jpc/dispatcher.rb', line 12

def unsubscribe(ws, channel)
  raise "Channel #{channel} not found" unless channels[channel.to_sym]

  channels[channel.to_sym].each_with_index do |object, index|
    channels[channel.to_sym].delete_at(index) if object == ws
  end

  { channel: channel, status: 'unsubscribed' }
end