Sha256: a5af9f54b1045fab70c62e0bf9c8a23c6a67d3421da6ec9e73255d30f4791ba8

Contents?: true

Size: 1.49 KB

Versions: 7

Compression:

Stored size: 1.49 KB

Contents

module FayeRails
  class Controller
    class Channel

      attr_reader :channel, :endpoint

      def initialize(channel, endpoint=nil)
        @channel = channel
        @endpoint = endpoint
      end

      def client
        FayeRails.client(endpoint)
      end

      def publish(message)
        client.publish(channel, message)
      end

      def monitor(event, &block)
        raise ArgumentError, "Unknown event #{event.inspect}" unless [:subscribe,:unsubscribe,:publish].member? event

        FayeRails.server(endpoint).bind(event) do |*args|
          Monitor.new.tap do |m|
            m.client_id = args.shift
            m.channel = args.shift
            m.data = args.shift
            m.instance_eval(&block) if File.fnmatch(channel, m.channel)
          end
        end
      end

      def filter(direction=:any, &block)
        filter = FayeRails::Filter.new(channel, direction, block)
        server = FayeRails.server(endpoint)
        server.add_extension(filter)
        filter.server = server
        filter
      end

      def subscribe(&block)
        EM.schedule do
          @subscription = FayeRails.client(endpoint).subscribe(channel) do |message|
            Message.new.tap do |m|
              m.message = message
              m.channel = channel
              m.instance_eval(&block)
            end
          end
        end
      end

      def unsubscribe
        EM.schedule do
          FayeRails.client(endpoint).unsubscribe(@subscription)
        end
      end

    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
faye-rails-2.0.0 lib/faye-rails/controller/channel.rb
faye-rails-1.0.10 lib/faye-rails/controller/channel.rb
faye-rails-1.0.8 lib/faye-rails/controller/channel.rb
faye-rails-1.0.7 lib/faye-rails/controller/channel.rb
faye-rails-1.0.6 lib/faye-rails/controller/channel.rb
faye-rails-1.0.5 lib/faye-rails/controller/channel.rb
faye-rails-1.0.4 lib/faye-rails/controller/channel.rb