Sha256: 7b80f0added3d55f1995beb8cc7d19a337ebbfdd3a598a2cb3610555b42866b8
Contents?: true
Size: 1.98 KB
Versions: 3
Compression:
Stored size: 1.98 KB
Contents
module Faye class Transport::Http < Transport def self.usable?(dispatcher, endpoint, &callback) callback.call(URI === endpoint) end def encode(messages) Faye.to_json(messages) end def request(messages) content = encode(messages) params = build_params(content) request = create_request(params) request.callback do handle_response(messages, request.response) store_cookies(request.response_header['SET_COOKIE']) end request.errback do handle_error(messages) end request end private def build_params(content) params = { :head => { 'Content-Length' => content.bytesize, 'Content-Type' => 'application/json', 'Host' => @endpoint.host + (@endpoint.port ? ":#{@endpoint.port}" : '') }.merge(@dispatcher.headers), :body => content, :timeout => -1 # for em-http-request < 1.0 } cookie = get_cookies params[:head]['Cookie'] = cookie unless cookie == '' params end def create_request(params) version = EventMachine::HttpRequest::VERSION.split('.')[0].to_i options = { :inactivity_timeout => 0, :tls => {:sni_hostname => @endpoint.hostname} } if @proxy[:origin] uri = URI.parse(@proxy[:origin]) options[:proxy] = {:host => uri.host, :port => uri.port} if uri.user options[:proxy][:authorization] = [uri.user, uri.password] end end if version >= 1 client = EventMachine::HttpRequest.new(@endpoint.to_s, options) else client = EventMachine::HttpRequest.new(@endpoint.to_s) end client.post(params) end def handle_response(messages, response) replies = MultiJson.load(response) rescue nil if replies receive(replies) else handle_error(messages) end end end Transport.register 'long-polling', Transport::Http end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
faye-1.2.2 | lib/faye/transport/http.rb |
faye-1.2.1 | lib/faye/transport/http.rb |
faye-1.2.0 | lib/faye/transport/http.rb |