Sha256: 8dbc9900863febe2451e5babe6843841a597b1204846f83a9676da9d88296409

Contents?: true

Size: 950 Bytes

Versions: 4

Compression:

Stored size: 950 Bytes

Contents

module WebsocketRails
  class Channel

    include Logging

    attr_reader :name, :subscribers

    def initialize(channel_name)
      @subscribers = []
      @name        = channel_name
      @private     = false
    end

    def subscribe(connection)
      log "#{connection} subscribed to channel #{name}"
      @subscribers << connection
    end

    def trigger(event_name,data={},options={})
      options.merge! :channel => name
      options[:data] = data

      event = Event.new event_name, options

      send_data event
    end

    def trigger_event(event)
      send_data event
    end

    def make_private
      @private = true
    end

    def is_private?
      @private
    end

    private

    def send_data(event)
      if WebsocketRails.synchronize? && event.server_token.nil?
        Synchronization.publish event
      end

      subscribers.each do |subscriber|
        subscriber.trigger event
      end
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
websocket-rails-0.3.0 lib/websocket_rails/channel.rb
websocket-rails-0.2.1 lib/websocket_rails/channel.rb
websocket-rails-0.2.0 lib/websocket_rails/channel.rb
websocket-rails-0.1.9 lib/websocket_rails/channel.rb