lib/sinatra/cometio/client.rb in sinatra-cometio-0.1.5 vs lib/sinatra/cometio/client.rb in sinatra-cometio-0.1.6

- old
+ new

@@ -13,10 +13,11 @@ def initialize(url) raise ArgumentError, "invalid URL (#{url})" unless url.kind_of? String and url =~ /^https?:\/\/.+/ @url = url @session = nil + @running = false end def push(type, data) begin res = HTTParty.post @url, :body => {:type => type, :data => data, :session => @session} @@ -26,21 +27,28 @@ emit :error, "CometIO push error" unless res.code == 200 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 Thread.new do - loop do + while @running do begin res = HTTParty.get "#{@url}?session=#{@session}", :timeout => 60000 unless res.code == 200 self.emit :error, "CometIO get error" sleep 10