class WebsocketRails::ConnectionAdapters::Http

Constants

HttpHeaders
TAIL
TERM

Attributes

headers[RW]

Public Class Methods

accepts?(env) click to toggle source
# File lib/websocket_rails/connection_adapters/http.rb, line 11
def self.accepts?(env)
  true
end
new(env,dispatcher) click to toggle source
# File lib/websocket_rails/connection_adapters/http.rb, line 17
def initialize(env,dispatcher)
  super
  @body = DeferrableBody.new
  @headers = HttpHeaders

  define_deferrable_callbacks

  origin = "#{request.protocol}#{request.raw_host_with_port}"
  @headers.merge!({'Access-Control-Allow-Origin' => origin}) if WebsocketRails.config.allowed_origins.include?(origin)
  # IE < 10.0 hack
  # XDomainRequest will not bubble up notifications of download progress in the first 2kb of the response
  # http://blogs.msdn.com/b/ieinternals/archive/2010/04/06/comet-streaming-in-internet-explorer-with-xmlhttprequest-and-xdomainrequest.aspx
  @body.chunk(encode_chunk(" " * 2048))

  EM.next_tick do
    @env['async.callback'].call [200, @headers, @body]
    on_open
  end
end

Public Instance Methods

close!() click to toggle source
# File lib/websocket_rails/connection_adapters/http.rb, line 41
def close!
  @body.close!
end
send(message) click to toggle source
# File lib/websocket_rails/connection_adapters/http.rb, line 37
def send(message)
  @body.chunk encode_chunk( message )
end

Private Instance Methods

define_deferrable_callbacks() click to toggle source
# File lib/websocket_rails/connection_adapters/http.rb, line 47
def define_deferrable_callbacks
  @body.callback do |event|
    on_close(event)
  end
  @body.errback do |event|
    on_close(event)
  end
end
encode_chunk(c) click to toggle source

From [Rack::Stream](github.com/intridea/rack-stream)

# File lib/websocket_rails/connection_adapters/http.rb, line 57
def encode_chunk(c)
  return nil if c.nil?
  # hack to work with Rack::File for now, should not TE chunked
  # things that aren't strings or respond to bytesize
  c = ::File.read(c.path) if c.kind_of?(Rack::File)
  size = Rack::Utils.bytesize(c)
  return nil if size == 0
  c.dup.force_encoding(Encoding::BINARY) if c.respond_to?(:force_encoding)
  [size.to_s(16), TERM, c, TERM].join
end