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



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/jpc/dispatcher.rb', line 19

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



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

def subscribe(ws, channel)
  channels[channel.to_sym] ||= []
  channels[channel.to_sym] << ws

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

#unsubscribe(ws, channel) ⇒ Object



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

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

  channels[channel.to_sym] -= [ws]

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