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}
SECURE_PROTOCOLS =
['https', 'wss']

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.



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

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

  scheme = @endpoint.respond_to?(:scheme) ? @endpoint.scheme : nil

  @proxy[:origin] ||= SECURE_PROTOCOLS.include?(scheme) ?
                      (ENV['HTTPS_PROXY'] || ENV['https_proxy']) :
                      (ENV['HTTP_PROXY']  || ENV['http_proxy'])
end

Class Attribute Details

.connection_typeObject

Returns the value of attribute connection_type.



124
125
126
# File 'lib/faye/transport/transport.rb', line 124

def connection_type
  @connection_type
end

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



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

def endpoint
  @endpoint
end

Class Method Details

.connection_typesObject



160
161
162
# File 'lib/faye/transport/transport.rb', line 160

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

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



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/faye/transport/transport.rb', line 126

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



155
156
157
158
# File 'lib/faye/transport/transport.rb', line 155

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

Instance Method Details

#batching?Boolean

Returns:

  • (Boolean)


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

def batching?
  true
end

#closeObject



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

def close
end

#connection_typeObject



38
39
40
# File 'lib/faye/transport/transport.rb', line 38

def connection_type
  self.class.connection_type
end

#encode(messages) ⇒ Object



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

def encode(messages)
  ''
end

#send_message(message) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/faye/transport/transport.rb', line 42

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
  @promise ||= EventMachine::DefaultDeferrable.new
  flush_large_batch

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

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

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