Class: WebSocket::Driver::Client

Inherits:
Hybi show all
Defined in:
lib/websocket/driver/client.rb

Constant Summary

Constants inherited from Hybi

Hybi::BYTE, Hybi::ERRORS, Hybi::ERROR_CODES, Hybi::FIN, Hybi::FRAGMENTED_OPCODES, Hybi::GUID, Hybi::LENGTH, Hybi::MAX_RESERVED_ERROR, Hybi::MIN_RESERVED_ERROR, Hybi::OPCODE, Hybi::OPCODES, Hybi::OPCODE_CODES, Hybi::OPENING_OPCODES, Hybi::RSV1, Hybi::RSV2, Hybi::RSV3

Constants inherited from WebSocket::Driver

MAX_LENGTH, STATES

Instance Attribute Summary collapse

Attributes inherited from WebSocket::Driver

#protocol, #ready_state

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Hybi

#binary, #close, #frame, generate_accept, #ping, #text

Methods inherited from WebSocket::Driver

#binary, #close, #ping, #set_header, #state, #text

Methods included from EventEmitter

#add_listener, #emit, #listener_count, #listeners, #on, #remove_all_listeners, #remove_listener

Constructor Details

#initialize(socket, options = {}) ⇒ Client

Returns a new instance of Client.



11
12
13
14
15
16
17
18
# File 'lib/websocket/driver/client.rb', line 11

def initialize(socket, options = {})
  super

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

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



9
10
11
# File 'lib/websocket/driver/client.rb', line 9

def headers
  @headers
end

#statusObject (readonly)

Returns the value of attribute status.



9
10
11
# File 'lib/websocket/driver/client.rb', line 9

def status
  @status
end

Class Method Details

.generate_keyObject



5
6
7
# File 'lib/websocket/driver/client.rb', line 5

def self.generate_key
  Base64.encode64((1..16).map { rand(255).chr } * '').strip
end

Instance Method Details

#parse(buffer) ⇒ Object



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

def parse(buffer)
  return super if @ready_state > 0
  @http.parse(buffer)
  return fail_handshake('Invalid HTTP response') if @http.error?
  validate_handshake if @http.complete?
  parse(@http.body) if @ready_state == 1
end

#startObject



24
25
26
27
28
29
# File 'lib/websocket/driver/client.rb', line 24

def start
  return false unless @ready_state == -1
  @socket.write(Driver.encode(handshake_request, :binary))
  @ready_state = 0
  true
end

#versionObject



20
21
22
# File 'lib/websocket/driver/client.rb', line 20

def version
  'hybi-13'
end