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/websocket/adapter.rb,
lib/faye/adapters/rainbows.rb,
lib/faye/adapters/rainbows_client.rb

Defined Under Namespace

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

Constant Summary collapse

ADAPTERS =
{
  'goliath'  => :Goliath,
  'rainbows' => :Rainbows,
  'thin'     => :Thin
}

Constants included from API

API::CLOSED, 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.


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

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


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

def env
  @env
end

Class Method Details

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


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

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


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

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

.load_adapter(backend) ⇒ Object


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

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)

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

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)

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

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

Instance Method Details

#rack_responseObject


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

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

#start_driverObject


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

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