Class: Faye::Connection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
EventMachine::Deferrable, Observable
Defined in:
lib/faye/connection.rb

Constant Summary collapse

MAX_DELAY =
0.1
INTERVAL =
1.0
TIMEOUT =
60.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, options = {}) ⇒ Connection

Returns a new instance of Connection.



15
16
17
18
19
20
# File 'lib/faye/connection.rb', line 15

def initialize(id, options = {})
  @id       = id
  @options  = options
  @channels = Set.new
  @inbox    = Set.new
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



13
14
15
# File 'lib/faye/connection.rb', line 13

def id
  @id
end

Instance Method Details

#connect(&block) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/faye/connection.rb', line 43

def connect(&block)
  callback(&block)
  return if @connected
  
  @connected = true
  
  if @deletion_timeout
    cancel_timer(@deletion_timeout)
    @deletion_timeout = nil
  end
  
  begin_delivery_timeout!
  begin_connection_timeout!
end

#disconnect!Object



69
70
71
72
# File 'lib/faye/connection.rb', line 69

def disconnect!
  unsubscribe(:all)
  flush!
end

#flush!Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/faye/connection.rb', line 58

def flush!
  return unless @connected
  release_connection!
  
  events = @inbox.entries
  @inbox = Set.new
  
  set_deferred_status(:succeeded, events)
  set_deferred_status(:deferred)
end

#subscribe(channel) ⇒ Object



32
33
34
# File 'lib/faye/connection.rb', line 32

def subscribe(channel)
  channel.add_observer(self) if @channels.add?(channel)
end

#timeoutObject



22
23
24
# File 'lib/faye/connection.rb', line 22

def timeout
  @options[:timeout] || TIMEOUT
end

#unsubscribe(channel) ⇒ Object



36
37
38
39
40
41
# File 'lib/faye/connection.rb', line 36

def unsubscribe(channel)
  return @channels.each(&method(:unsubscribe)) if channel == :all
  return unless @channels.member?(channel)
  @channels.delete(channel)
  channel.delete_observer(self)
end

#update(message, event) ⇒ Object



26
27
28
29
30
# File 'lib/faye/connection.rb', line 26

def update(message, event)
  return unless message == :message
  @inbox.add(event)
  begin_delivery_timeout! if @connected
end