Class: Faye::Transport

Inherits:
Object
  • Object
show all
Includes:
Logging, Publisher, Timeouts
Defined in:
lib/faye/transport/transport.rb

Direct Known Subclasses

WebSocket, Transport::Http, Transport::Local

Defined Under Namespace

Classes: Http, Local, WebSocket

Constant Summary

Constants included from Logging

Logging::DEFAULT_LOG_LEVEL, Logging::LOG_LEVELS

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes included from Logging

#log_level

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Timeouts

#add_timeout, #remove_timeout

Methods included from Publisher

#bind, #count_listeners, #trigger, #unbind

Methods included from Logging

#log

Constructor Details

#initialize(client, endpoint) ⇒ Transport

Returns a new instance of Transport.



10
11
12
13
14
15
# File 'lib/faye/transport/transport.rb', line 10

def initialize(client, endpoint)
  debug('Created new ? transport for ?', connection_type, endpoint)
  @client   = client
  @endpoint = endpoint
  @outbox   = []
end

Class Attribute Details

.connection_typeObject

Returns the value of attribute connection_type.



72
73
74
# File 'lib/faye/transport/transport.rb', line 72

def connection_type
  @connection_type
end

Instance Attribute Details

#cookiesObject

Returns the value of attribute cookies.



8
9
10
# File 'lib/faye/transport/transport.rb', line 8

def cookies
  @cookies
end

#headersObject

Returns the value of attribute headers.



8
9
10
# File 'lib/faye/transport/transport.rb', line 8

def headers
  @headers
end

Class Method Details

.get(client, connection_types = nil, &callback) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/faye/transport/transport.rb', line 74

def get(client, connection_types = nil, &callback)
  endpoint = client.endpoint
  connection_types ||= supported_connection_types
  
  select = lambda do |(conn_type, klass), resume|
    if connection_types.include?(conn_type)
      klass.usable?(endpoint) do |is_usable|
        if is_usable
          callback.call(klass.new(client, endpoint))
        else
          resume.call
        end
      end
    else
      resume.call
    end
  end
  
  error = lambda do
    raise "Could not find a usable connection type for #{ endpoint }"
  end
  
  Faye.async_each(@transports, select, error)
end

.register(type, klass) ⇒ Object



99
100
101
102
# File 'lib/faye/transport/transport.rb', line 99

def register(type, klass)
  @transports << [type, klass]
  klass.connection_type = type
end

.supported_connection_typesObject



104
105
106
# File 'lib/faye/transport/transport.rb', line 104

def supported_connection_types
  @transports.map { |t| t.first }
end

Instance Method Details

#batching?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/faye/transport/transport.rb', line 17

def batching?
  true
end

#closeObject



21
22
# File 'lib/faye/transport/transport.rb', line 21

def close
end

#connection_typeObject



24
25
26
# File 'lib/faye/transport/transport.rb', line 24

def connection_type
  self.class.connection_type
end

#flushObject



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

def flush
  remove_timeout(:publish)

  if @outbox.size > 1 and @connection_message
    @connection_message['advice'] = {'timeout' => 0}
  end

  request(@outbox, @timeout)

  @connection_message = nil
  @outbox = []
end

#receive(responses) ⇒ Object



58
59
60
61
# File 'lib/faye/transport/transport.rb', line 58

def receive(responses)
  debug('Client ? received from ?: ?', @client.client_id, @endpoint, responses)
  responses.each { |response| @client.receive_message(response) }
end

#retry_block(message, timeout) ⇒ Object



63
64
65
66
67
# File 'lib/faye/transport/transport.rb', line 63

def retry_block(message, timeout)
  lambda do
    EventMachine.add_timer(@client.retry) { request(message, timeout) }
  end
end

#send(message, timeout) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/faye/transport/transport.rb', line 28

def send(message, timeout)
  debug('Client ? sending message to ?: ?', @client.client_id, @endpoint, message)

  return request([message], timeout) unless batching?

  @outbox << message
  @timeout = timeout

  return flush if message['channel'] == Channel::HANDSHAKE

  if message['channel'] == Channel::CONNECT
    @connection_message = message
  end

  add_timeout(:publish, Engine::MAX_DELAY) { flush }
end