Sha256: 9ad5e8a62730d6d2d4f6de8863bde593e1c34966d7001d3e18d1a0515645ffc8

Contents?: true

Size: 1.33 KB

Versions: 10

Compression:

Stored size: 1.33 KB

Contents

module WebsocketRails
  class Channel

    include Logging

    delegate :config, :to => WebsocketRails

    attr_reader :name, :subscribers

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

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

    def unsubscribe(connection)
      return unless @subscribers.include? connection
      info "#{connection} unsubscribed from channel #{name}"
      @subscribers.delete connection
    end

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

      event = Event.new event_name, options

      info "[#{name}] #{event.data.inspect}"
      send_data event
    end

    def trigger_event(event)
      info "[#{name}] #{event.data.inspect}"
      send_data event
    end

    def make_private
      unless config.keep_subscribers_when_private?
        @subscribers.clear
      end
      @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

10 entries across 10 versions & 1 rubygems

Version Path
websocket-rails-0.6.2 lib/websocket_rails/channel.rb
websocket-rails-0.6.1 lib/websocket_rails/channel.rb
websocket-rails-0.6.0 lib/websocket_rails/channel.rb
websocket-rails-0.5.0 lib/websocket_rails/channel.rb
websocket-rails-0.4.9 lib/websocket_rails/channel.rb
websocket-rails-0.4.8 lib/websocket_rails/channel.rb
websocket-rails-0.4.7 lib/websocket_rails/channel.rb
websocket-rails-0.4.6 lib/websocket_rails/channel.rb
websocket-rails-0.4.5 lib/websocket_rails/channel.rb
websocket-rails-0.4.4 lib/websocket_rails/channel.rb