Class: Faye::Connection

Inherits:
Object
  • Object
show all
Includes:
EventMachine::Deferrable, Publisher, Timeouts
Defined in:
lib/faye/protocol/connection.rb

Constant Summary collapse

MAX_DELAY =
0.001
INTERVAL =
0.0
TIMEOUT =
60.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Timeouts

#add_timeout, #remove_timeout

Methods included from Publisher

#add_subscriber, #count_subscribers, #publish_event, #remove_subscriber

Constructor Details

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

Returns a new instance of Connection.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/faye/protocol/connection.rb', line 13

def initialize(id, options = {})
  @id        = id
  @options   = options
  @interval  = @options[:interval] || INTERVAL
  @timeout   = @options[:timeout] || TIMEOUT
  @channels  = Set.new
  @inbox     = Set.new
  @connected = false
  
  begin_deletion_timeout
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



11
12
13
# File 'lib/faye/protocol/connection.rb', line 11

def id
  @id
end

#intervalObject (readonly)

Returns the value of attribute interval.



11
12
13
# File 'lib/faye/protocol/connection.rb', line 11

def interval
  @interval
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



11
12
13
# File 'lib/faye/protocol/connection.rb', line 11

def timeout
  @timeout
end

Instance Method Details

#connect(options, &block) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/faye/protocol/connection.rb', line 48

def connect(options, &block)
  options = options || {}
  timeout = options['timeout'] ? options['timeout'] / 1000.0 : @timeout
  
  set_deferred_status(:deferred)
  
  callback(&block)
  return if @connected
  
  @connected = true
  remove_timeout(:deletion)
  
  begin_delivery_timeout
  begin_connection_timeout(timeout)
end

#disconnect!Object



75
76
77
78
# File 'lib/faye/protocol/connection.rb', line 75

def disconnect!
  unsubscribe(:all)
  flush!
end

#flush!Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/faye/protocol/connection.rb', line 64

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

#on_message(event) ⇒ Object



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

def on_message(event)
  return unless @inbox.add?(event)
  @socket.send(JSON.unparse(event)) if @socket
  begin_delivery_timeout
end

#socket=(socket) ⇒ Object



25
26
27
28
# File 'lib/faye/protocol/connection.rb', line 25

def socket=(socket)
  @connected = true
  @socket    = socket
end

#subscribe(channel) ⇒ Object



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

def subscribe(channel)
  return unless @channels.add?(channel)
  channel.add_subscriber(:message, method(:on_message))
end

#unsubscribe(channel) ⇒ Object



41
42
43
44
45
46
# File 'lib/faye/protocol/connection.rb', line 41

def unsubscribe(channel)
  return @channels.each(&method(:unsubscribe)) if channel == :all
  return unless @channels.member?(channel)
  @channels.delete(channel)
  channel.remove_subscriber(:message, method(:on_message))
end