Class: Sinatra::CometIO::Client
- Inherits:
-
Object
- Object
- Sinatra::CometIO::Client
- Includes:
- EventEmitter
- Defined in:
- lib/sinatra/cometio/client.rb
Defined Under Namespace
Classes: Error
Instance Attribute Summary collapse
-
#session ⇒ Object
readonly
Returns the value of attribute session.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
- #close ⇒ Object
- #connect ⇒ Object
-
#initialize(url) ⇒ Client
constructor
A new instance of Client.
- #push(type, data) ⇒ Object
Constructor Details
#initialize(url) ⇒ Client
Returns a new instance of Client.
16 17 18 19 20 21 22 |
# File 'lib/sinatra/cometio/client.rb', line 16 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 |
Instance Attribute Details
#session ⇒ Object (readonly)
Returns the value of attribute session.
13 14 15 |
# File 'lib/sinatra/cometio/client.rb', line 13 def session @session end |
#timeout ⇒ Object
Returns the value of attribute timeout.
14 15 16 |
# File 'lib/sinatra/cometio/client.rb', line 14 def timeout @timeout end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
13 14 15 |
# File 'lib/sinatra/cometio/client.rb', line 13 def url @url end |
Instance Method Details
#close ⇒ Object
50 51 52 53 |
# File 'lib/sinatra/cometio/client.rb', line 50 def close @running = false self.remove_listener :__session_id end |
#connect ⇒ Object
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/sinatra/cometio/client.rb', line 39 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 |
#push(type, data) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/sinatra/cometio/client.rb', line 24 def push(type, data) post_data = { :json => { :session => @session, :events => [{:type => type, :data => data}] }.to_json } begin res = HTTParty.post @url, :timeout => 10, :body => post_data emit :error, "CometIO push error" unless res.code == 200 rescue StandardError, Timeout::Error => e emit :error, "CometIO push error" end end |