Class: Faye::WebSocket

Inherits:
Object
  • Object
show all
Includes:
API
Defined in:
lib/faye/websocket.rb,
lib/faye/websocket/api.rb,
lib/faye/websocket/client.rb,
lib/faye/adapters/rainbows.rb,
lib/faye/websocket/adapter.rb,
lib/faye/websocket/ssl_verifier.rb,
lib/faye/adapters/rainbows_client.rb

Defined Under Namespace

Modules: API, Adapter Classes: Client, RainbowsClient, SslVerifier, Stream

Constant Summary collapse

ADAPTERS =
{
  'goliath'  => :Goliath,
  'rainbows' => :Rainbows,
  'thin'     => :Thin
}
SSLError =
Class.new(OpenSSL::SSL::SSLError)

Constants included from API

API::CLOSED, API::CLOSE_TIMEOUT, API::CLOSING, API::CONNECTING, API::OPEN, API::TYPES

Instance Attribute Summary collapse

Attributes included from API

#buffered_amount, #ready_state, #url

Class Method Summary collapse

Instance Method Summary collapse

Methods included from API

#close, #ping, #protocol, #send, #write

Methods included from API::EventTarget

#add_event_listener, #add_listener, #dispatch_event, #remove_event_listener

Constructor Details

#initialize(env, protocols = nil, options = {}) ⇒ WebSocket

Returns a new instance of WebSocket.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/faye/websocket.rb', line 69

def initialize(env, protocols = nil, options = {})
  WebSocket.ensure_reactor_running

  @env = env
  @url = WebSocket.determine_url(@env)

  super(options) { ::WebSocket::Driver.rack(self, :max_length => options[:max_length], :protocols => protocols) }
  @driver_started = false

  @stream = Stream.new(self)

  if callback = @env['async.callback']
    callback.call([101, {}, @stream])
  end
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



66
67
68
# File 'lib/faye/websocket.rb', line 66

def env
  @env
end

Class Method Details

.determine_url(env, schemes = ['wss', 'ws']) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/faye/websocket.rb', line 31

def self.determine_url(env, schemes = ['wss', 'ws'])
  scheme = schemes[secure_request?(env) ? 0 : 1]
  host   = env['HTTP_HOST']
  path   = env['PATH_INFO']
  query  = env['QUERY_STRING'].to_s

  scheme + '://' + host + path + (query.empty? ? '' : '?' + query)
end

.ensure_reactor_runningObject



40
41
42
43
# File 'lib/faye/websocket.rb', line 40

def self.ensure_reactor_running
  Thread.new { EventMachine.run } unless EventMachine.reactor_running?
  Thread.pass until EventMachine.reactor_running?
end

.load_adapter(backend) ⇒ Object



45
46
47
48
49
50
# File 'lib/faye/websocket.rb', line 45

def self.load_adapter(backend)
  const = Kernel.const_get(ADAPTERS[backend]) rescue nil
  require(backend) unless const
  path = File.expand_path("../adapters/#{ backend }.rb", __FILE__)
  require(path) if File.file?(path)
end

.secure_request?(env) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
58
59
60
# File 'lib/faye/websocket.rb', line 52

def self.secure_request?(env)
  return true if env['HTTPS'] == 'on'
  return true if env['HTTP_X_FORWARDED_SSL'] == 'on'
  return true if env['HTTP_X_FORWARDED_SCHEME'] == 'https'
  return true if env['HTTP_X_FORWARDED_PROTO'] == 'https'
  return true if env['rack.url_scheme'] == 'https'

  return false
end

.websocket?(env) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/faye/websocket.rb', line 62

def self.websocket?(env)
  ::WebSocket::Driver.websocket?(env)
end

Instance Method Details

#rack_responseObject



91
92
93
94
# File 'lib/faye/websocket.rb', line 91

def rack_response
  start_driver
  [ -1, {}, [] ]
end

#start_driverObject



85
86
87
88
89
# File 'lib/faye/websocket.rb', line 85

def start_driver
  return if @driver.nil? || @driver_started
  @driver_started = true
  EventMachine.schedule { @driver.start }
end