Class: EventMachine::CometIO::Client

Inherits:
Object
  • Object
show all
Includes:
EventEmitter
Defined in:
lib/em-cometio-client.rb,
lib/em-cometio-client/client.rb,
lib/em-cometio-client/version.rb

Constant Summary collapse

VERSION =
"0.1.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
18
# File 'lib/em-cometio-client/client.rb', line 8

def initialize(url)
  raise ArgumentError, "invalid URL (#{url})" unless url.kind_of? String and url =~ /^https?:\/\/.+/
  @url = url
  @session = nil
  @running = false
  @timeout = 120
  @post_queue = []
  EM::add_periodic_timer 1 do
    flush
  end
end

Instance Attribute Details

#sessionObject (readonly)

Returns the value of attribute session.



5
6
7
# File 'lib/em-cometio-client/client.rb', line 5

def session
  @session
end

#timeoutObject

Returns the value of attribute timeout.



6
7
8
# File 'lib/em-cometio-client/client.rb', line 6

def timeout
  @timeout
end

#urlObject (readonly)

Returns the value of attribute url.



5
6
7
# File 'lib/em-cometio-client/client.rb', line 5

def url
  @url
end

Instance Method Details

#closeObject



55
56
57
58
# File 'lib/em-cometio-client/client.rb', line 55

def close
  @running = false
  self.remove_listener :__session_id
end

#connectObject



44
45
46
47
48
49
50
51
52
53
# File 'lib/em-cometio-client/client.rb', line 44

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



36
37
38
39
40
41
42
# File 'lib/em-cometio-client/client.rb', line 36

def push(type, data)
  if !@running or !@session
    emit :error, "CometIO not connected"
    return
  end
  @post_queue.push :type => type, :data => data
end