Sha256: 37a9f88885f557b1c65d949f277b921ab1b5e64b19c179524b62694b747cf2e0

Contents?: true

Size: 1.39 KB

Versions: 3

Compression:

Stored size: 1.39 KB

Contents

require 'eventmachine'
require 'json'
require 'digest/md5'
require 'event_emitter'
require 'sinatra/streaming'
require File.expand_path 'application', File.dirname(__FILE__)
require File.expand_path '../sinatra-cometio/version', File.dirname(__FILE__)

class CometIO
  def self.sessions
    @@sessions ||= Hash.new{|h,session_id|
      h[session_id] = {
        :queue => [{:type => :__session_id, :data => session_id}],
        :stream => nil,
        :last => nil
      }
    }
  end

  def self.gc
    self.sessions.each do |id, s|
      next unless s[:last] and s[:last] < Time.now-60
      self.sessions.delete id rescue next
    end
  end

  EM::defer do
    loop do
      self.gc
      sleep 60
    end
  end

  def self.push(type, data, opt={})
    if opt[:to]
      self.sessions[opt[:to].to_s][:queue].push :type => type, :data => data
    else
      self.sessions.each do |id,s|
        if s[:queue].empty? and s[:stream] != nil
          begin
            s[:stream].write({:type => type, :data => data}.to_json)
            s[:stream].flush
            s[:stream].close
          rescue
            s[:stream].class
            s[:queue].push :type => type, :data => data
          end
        else
          s[:queue].push :type => type, :data => data
        end
      end
    end
  end

  def self.create_session
    Digest::MD5.hexdigest "#{Time.now.to_i}_#{Time.now.usec}"
  end
end
EventEmitter.apply CometIO

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
sinatra-cometio-0.1.1 lib/sinatra/cometio.rb
sinatra-cometio-0.1.0 lib/sinatra/cometio.rb
sinatra-cometio-0.0.9 lib/sinatra/cometio.rb