class Faye::WebSocket::Client

Constants

DEFAULT_PORTS
SECURE_PROTOCOLS

Public Class Methods

new(url, protocols = nil, options = {}) click to toggle source
Calls superclass method Faye::WebSocket::API::new
# File lib/faye/websocket/client.rb, line 15
def initialize(url, protocols = nil, options = {})
  @url = url
  super(options) { ::WebSocket::Driver.client(self, :max_length => options[:max_length], :protocols => protocols) }

  proxy       = options.fetch(:proxy, {})
  @endpoint   = URI.parse(proxy[:origin] || @url)
  port        = @endpoint.port || DEFAULT_PORTS[@endpoint.scheme]
  @origin_tls = options.fetch(:tls, {})
  @socket_tls = proxy[:origin] ? proxy.fetch(:tls, {}) : @origin_tls

  configure_proxy(proxy)

  EventMachine.connect(@endpoint.host, port, Connection) do |conn|
    conn.parent = self
  end
rescue => error
  on_network_error(error)
end

Private Instance Methods

configure_proxy(proxy) click to toggle source
# File lib/faye/websocket/client.rb, line 36
def configure_proxy(proxy)
  return unless proxy[:origin]

  @proxy = @driver.proxy(proxy[:origin])
  @proxy.on(:error) { |error| @driver.emit(:error, error) }

  if headers = proxy[:headers]
    headers.each { |name, value| @proxy.set_header(name, value) }
  end

  @proxy.on(:connect) do
    @proxy = nil
    start_tls(URI.parse(@url), @origin_tls)
    @driver.start
  end
end
on_connect(stream) click to toggle source
# File lib/faye/websocket/client.rb, line 61
def on_connect(stream)
  @stream = stream
  start_tls(@endpoint, @socket_tls)

  worker = @proxy || @driver
  worker.start
end
on_network_error(error) click to toggle source
# File lib/faye/websocket/client.rb, line 69
def on_network_error(error)
  emit_error("Network error: #{ @url }: #{ error.message }")
  finalize_close
end
ssl_handshake_completed() click to toggle source
# File lib/faye/websocket/client.rb, line 80
def ssl_handshake_completed
  @ssl_verifier.ssl_handshake_completed
rescue => error
  on_network_error(error)
end
ssl_verify_peer(cert) click to toggle source
# File lib/faye/websocket/client.rb, line 74
def ssl_verify_peer(cert)
  @ssl_verifier.ssl_verify_peer(cert)
rescue => error
  on_network_error(error)
end
start_tls(uri, options) click to toggle source
# File lib/faye/websocket/client.rb, line 53
def start_tls(uri, options)
  return unless SECURE_PROTOCOLS.include?(uri.scheme)

  tls_options = { :sni_hostname => uri.host, :verify_peer => true }.merge(options)
  @ssl_verifier = SslVerifier.new(uri.host, tls_options)
  @stream.start_tls(tls_options)
end