class WebSocket::Driver::Client

Constants

VALID_SCHEMES

Attributes

headers[R]
status[R]

Public Instance Methods

parse(chunk) click to toggle source
# File lib/websocket/driver/client.rb, line 61
def parse(chunk)
  return if @ready_state == 3
  return super if @ready_state > 0

  @http.parse(chunk)
  return fail_handshake('Invalid HTTP response') if @http.error?
  return unless @http.complete?

  validate_handshake
  return if @ready_state == 3

  open
  parse(@http.body)
end
proxy(origin, options = {}) click to toggle source
# File lib/websocket/driver/client.rb, line 50
def proxy(origin, options = {})
  Proxy.new(self, origin, options)
end
start() click to toggle source
# File lib/websocket/driver/client.rb, line 54
def start
  return false unless @ready_state == -1
  @socket.write(handshake_request)
  @ready_state = 0
  true
end
version() click to toggle source
# File lib/websocket/driver/client.rb, line 46
def version
  'hybi-13'
end

Public Class Methods

generate_key() click to toggle source
# File lib/websocket/driver/client.rb, line 7
def self.generate_key
  Base64.strict_encode64(SecureRandom.random_bytes(16))
end
new(socket, options = {}) click to toggle source
# File lib/websocket/driver/client.rb, line 13
def initialize(socket, options = {})
  super

  @ready_state = -1
  @key         = Client.generate_key
  @accept      = Hybi.generate_accept(@key)
  @http        = HTTP::Response.new

  uri = URI.parse(@socket.url)
  unless VALID_SCHEMES.include?(uri.scheme)
    raise URIError, "#{socket.url} is not a valid WebSocket URL"
  end

  host      = uri.host + (uri.port ? ":#{uri.port}" : '')
  path      = (uri.path == '') ? '/' : uri.path
  @pathname = path + (uri.query ? '?' + uri.query : '')

  @headers['Host']                  = host
  @headers['Upgrade']               = 'websocket'
  @headers['Connection']            = 'Upgrade'
  @headers['Sec-WebSocket-Key']     = @key
  @headers['Sec-WebSocket-Version'] = '13'

  if @protocols.size > 0
    @headers['Sec-WebSocket-Protocol'] = @protocols * ', '
  end

  if uri.user
    auth = Base64.strict_encode64([uri.user, uri.password] * ':')
    @headers['Authorization'] = 'Basic ' + auth
  end
end