Sha256: 4ecbbbfa10107d8907cc8012e0f0b789fedf2ddafed4531eac35df38465884f6

Contents?: true

Size: 725 Bytes

Versions: 2

Compression:

Stored size: 725 Bytes

Contents

class JPC::Dispatcher
  include JPC::Helpers

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

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

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

    channels[channel.to_sym] -= [ws]

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

  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

  private

  def channels
    @channels ||= {}
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
jpc-1.2.0 lib/jpc/dispatcher.rb
jpc-1.1.0 lib/jpc/dispatcher.rb