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 collapse

DEFAULT_PORTS =
{ 'http' => 80, 'https' => 433, 'ws' => 80, 'wss' => 443 }

Constants included from Logging

Logging::LOG_LEVELS

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Timeouts

#add_timeout, #remove_all_timeouts, #remove_timeout

Methods included from Publisher

#unbind

Constructor Details

#initialize(dispatcher, endpoint) ⇒ Transport

Returns a new instance of Transport.


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

def initialize(dispatcher, endpoint)
  super()
  @dispatcher = dispatcher
  @endpoint   = endpoint
  @outbox     = []
  @proxy      = @dispatcher.proxy.dup

  @proxy[:origin] ||= @endpoint.respond_to?(:find_proxy) ?
                      @endpoint.find_proxy :
                      nil
end

Class Attribute Details

.connection_typeObject

Returns the value of attribute connection_type


131
132
133
# File 'lib/faye/transport/transport.rb', line 131

def connection_type
  @connection_type
end

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint


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

def endpoint
  @endpoint
end

Class Method Details

.connection_typesObject


167
168
169
# File 'lib/faye/transport/transport.rb', line 167

def connection_types
  @transports.map { |t| t[0] }
end

.get(dispatcher, allowed, disabled, &callback) ⇒ Object


133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/faye/transport/transport.rb', line 133

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

  select = lambda do |(conn_type, klass), resume|
    conn_endpoint = dispatcher.endpoint_for(conn_type)

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

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

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


162
163
164
165
# File 'lib/faye/transport/transport.rb', line 162

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

Instance Method Details

#batching?Boolean

Returns:

  • (Boolean)

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

def batching?
  true
end

#closeObject


28
29
# File 'lib/faye/transport/transport.rb', line 28

def close
end

#connection_typeObject


35
36
37
# File 'lib/faye/transport/transport.rb', line 35

def connection_type
  self.class.connection_type
end

#encode(messages) ⇒ Object


31
32
33
# File 'lib/faye/transport/transport.rb', line 31

def encode(messages)
  ''
end

#send_message(message) ⇒ Object


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/faye/transport/transport.rb', line 39

def send_message(message)
  debug('Client ? sending message to ? via ?: ?', @dispatcher.client_id, @endpoint, connection_type, message)

  unless batching?
    promise = EventMachine::DefaultDeferrable.new
    promise.succeed(request([message]))
    return promise
  end

  @outbox << message
  flush_large_batch

  if message['channel'] == Channel::HANDSHAKE
    return publish(0.01)
  end

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

  publish(Engine::MAX_DELAY)
end