class Faye::EventSource

def initialize(env, options = {})

def initialize(env, options = {})
  WebSocket.ensure_reactor_running
  super()
  @env    = env
  @ping   = options[:ping]
  @retry  = (options[:retry] || DEFAULT_RETRY).to_f
  @url    = EventSource.determine_url(env)
  @stream = Stream.new(self)
  @ready_state = WebSocket::API::CONNECTING
  headers = ::WebSocket::Driver::Headers.new
  if options[:headers]
    options[:headers].each { |k,v| headers[k] = v }
  end
  if callback = @env['async.callback']
    callback.call([101, {}, @stream])
  end
  @stream.write("HTTP/1.1 200 OK\r\n" +
                "Content-Type: text/event-stream\r\n" +
                "Cache-Control: no-cache, no-store\r\n" +
                "Connection: close\r\n" +
                headers.to_s +
                "\r\n" +
                "retry: #{ (@retry * 1000).floor }\r\n\r\n")
  EventMachine.next_tick { open }
  if @ping
    @ping_timer = EventMachine.add_periodic_timer(@ping) { ping }
  end
end