Sha256: 93d2b61355b7103e4932eae3c019a0278ef91fd3f1ba8522374b4707cc6e0c1f
Contents?: true
Size: 1.66 KB
Versions: 1
Compression:
Stored size: 1.66 KB
Contents
module EventMachine module CometIO class Client include EventEmitter attr_reader :url, :session attr_accessor :timeout def initialize(url) raise ArgumentError, "invalid URL (#{url})" unless url.kind_of? String and url =~ /^https?:\/\/.+/ @url = url @session = nil @running = false @timeout = 120 end def push(type, data) that = self http = EM::HttpRequest.new(@url, :connect_timeout => 10). post(:body => {:type => type, :data => data, :session => @session}) http.callback do |res| end http.errback do |err| self.emit :error, "CometIO push error" end end def connect return self if @running self.on :__session_id do |session| @session = session self.emit :connect, @session end @running = true get return self end def close @running = false self.remove_listener :__session_id end private def get http = EM::HttpRequest.new("#{@url}?session=#{@session}", :connect_timeout => @timeout).get http.callback do |res| begin data_arr = JSON.parse res.response data_arr = [data_arr] unless data_arr.kind_of? Array data_arr.each do |data| self.emit data['type'], data['data'] end rescue JSON::ParserError rescue StandardError self.emit :error, "CometIO get error" sleep 10 end get end http.errback do |err| get end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
em-cometio-client-0.0.7 | lib/em-cometio-client/client.rb |