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
# File 'lib/faye/transport/transport.rb', line 10

def initialize(client, endpoint)
  @client   = client
  @endpoint = endpoint
  @outbox   = []
end

Class Attribute Details

.connection_typeObject

Returns the value of attribute connection_type.



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

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

#endpointObject

Returns the value of attribute endpoint.



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

def endpoint
  @endpoint
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, allowed, disabled, &callback) ⇒ Object



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

def get(client, allowed, disabled, &callback)
  endpoint = client.endpoint

  select = lambda do |(conn_type, klass), resume|
    conn_endpoint = client.endpoints[conn_type] || endpoint

    if disabled.include?(conn_type)
      next resume.call
    end

    unless allowed.include?(conn_type)
      klass.usable?(client, conn_endpoint) { |u| }
      next resume.call
    end

    klass.usable?(client, conn_endpoint) do |is_usable|
      next resume.call unless is_usable
      transport = klass.respond_to?(:create) ? klass.create(client, conn_endpoint) : klass.new(client, conn_endpoint)
      callback.call(transport)
    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



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

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

Instance Method Details

#batching?Boolean

Returns:

  • (Boolean)


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

def batching?
  true
end

#closeObject



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

def close
end

#connection_typeObject



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

def connection_type
  self.class.connection_type
end

#flushObject



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

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



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

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



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

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

#send(message, timeout) ⇒ Object



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

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

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

  @outbox << message
  @timeout = timeout

  if message['channel'] == Channel::HANDSHAKE
    return add_timeout(:publish, 0.01) { flush }
  end

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

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