Sha256: 79c35cb266b87be53f6e5ee8917c48b9c403f3e4ec58c4e36d3e1bf602d2847c

Contents?: true

Size: 1.87 KB

Versions: 7

Compression:

Stored size: 1.87 KB

Contents

EventEmitter.apply Sinatra::RocketIO

module Sinatra
  module RocketIO

    def self.push(type, data, opt={})
      if opt.include? :to and opt[:to].kind_of? Array
        opt[:to].each do |to|
          push type, data, :to => to
        end
      elsif opt.include? :channel
        channels.select{|session, channel|
          channel == opt[:channel].to_s
        }.each do |session|
          push type, data, :to => session
        end
      else
        if options[:websocket]
          Sinatra::WebSocketIO.push type, data, opt
        end
        if options[:comet]
          Sinatra::CometIO.push type, data, opt
        end
      end
    end

    def self.sessions
      {
        :websocket => Sinatra::WebSocketIO.sessions.keys,
        :comet => Sinatra::CometIO.sessions.keys
      }
    end

  end
end

Sinatra::RocketIO.once :start do
  if options[:comet]
    Sinatra::CometIO.on :* do |event_name, *args|
      event_name = :__connect if event_name == :connect
      if args.size > 1
        Sinatra::RocketIO.emit event_name, args[0], Sinatra::RocketIO::ClientInfo.new(:session => args[1], :channel => Sinatra::RocketIO.channels[args[1]], :type => :comet)
      else
        Sinatra::RocketIO.emit event_name, Sinatra::RocketIO::ClientInfo.new(:session => args[0], :channel => Sinatra::RocketIO.channels[args[0]], :type => :comet)
      end
    end
  end
  if options[:websocket]
    Sinatra::WebSocketIO.on :* do |event_name, *args|
      event_name = :__connect if event_name == :connect
      if args.size > 1
        Sinatra::RocketIO.emit event_name, args[0], Sinatra::RocketIO::ClientInfo.new(:session => args[1], :channel => Sinatra::RocketIO.channels[args[1]], :type => :websocket)
      else
        Sinatra::RocketIO.emit event_name, Sinatra::RocketIO::ClientInfo.new(:session => args[0], :channel => Sinatra::RocketIO.channels[args[0]], :type => :websocket)
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
sinatra-rocketio-0.2.6 lib/sinatra-rocketio/rocketio.rb
sinatra-rocketio-0.2.5 lib/sinatra-rocketio/rocketio.rb
sinatra-rocketio-0.2.4 lib/sinatra-rocketio/rocketio.rb
sinatra-rocketio-0.2.3 lib/sinatra-rocketio/rocketio.rb
sinatra-rocketio-0.2.2 lib/sinatra-rocketio/rocketio.rb
sinatra-rocketio-0.2.1 lib/sinatra-rocketio/rocketio.rb
sinatra-rocketio-0.2.0 lib/sinatra-rocketio/rocketio.rb