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]
  @secure     = SECURE_PROTOCOLS.include?(endpoint.scheme)
  @origin_tls = options.fetch(:tls, {})
  @socket_tls = proxy[:origin] ? proxy.fetch(:tls, {}) : @origin_tls

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

    @proxy.on(:connect) do
      uri    = URI.parse(@url)
      secure = SECURE_PROTOCOLS.include?(uri.scheme)
      @proxy = nil

      if secure
        origin_tls = {:sni_hostname => uri.host}.merge(@origin_tls)
        @stream.start_tls(origin_tls)
      end

      @driver.start
    end

    @proxy.on(:error) do |error|
      @driver.emit(:error, error)
    end
  end

  EventMachine.connect(endpoint.host, port, Connection) do |conn|
    conn.parent = self
  end
rescue => error
  emit_error("Network error: #{url}: #{error.message}")
  finalize_close
end

Private Instance Methods

on_connect(stream) click to toggle source
# File lib/faye/websocket/client.rb, line 60
def on_connect(stream)
  @stream = stream

  if @secure
    socket_tls = {:sni_hostname => URI.parse(@url).host}.merge(@socket_tls)
    @stream.start_tls(socket_tls)
  end

  worker = @proxy || @driver
  worker.start
end