Class: WebSocket::Driver::Client
- Inherits:
-
Hybi
- Object
- WebSocket::Driver
- Hybi
- WebSocket::Driver::Client
- Defined in:
- lib/websocket/driver/client.rb
Constant Summary
Constants inherited from Hybi
Hybi::BYTE, Hybi::ERRORS, Hybi::ERROR_CODES, Hybi::FIN, Hybi::GUID, Hybi::LENGTH, Hybi::MAX_RESERVED_ERROR, Hybi::MESSAGE_OPCODES, 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
Instance Attribute Summary collapse
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Attributes inherited from WebSocket::Driver
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(socket, options = {}) ⇒ Client
constructor
A new instance of Client.
- #parse(buffer) ⇒ Object
- #proxy(origin, options = {}) ⇒ Object
- #start ⇒ Object
- #version ⇒ Object
Methods inherited from Hybi
#add_extension, #binary, #close, #frame, generate_accept, #ping, #text
Methods inherited from WebSocket::Driver
#add_extension, #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 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/websocket/driver/client.rb', line 11 def initialize(socket, = {}) super @ready_state = -1 @key = Client.generate_key @accept = Hybi.generate_accept(@key) @http = HTTP::Response.new uri = URI.parse(@socket.url) 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.encode64([uri.user, uri.password] * ':').gsub(/\n/, '') @headers['Authorization'] = 'Basic ' + auth end end |
Instance Attribute Details
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
9 10 11 |
# File 'lib/websocket/driver/client.rb', line 9 def headers @headers end |
#status ⇒ Object (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_key ⇒ Object
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
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/websocket/driver/client.rb', line 55 def parse(buffer) return super if @ready_state > 0 @http.parse(buffer) 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 = {}) ⇒ Object
44 45 46 |
# File 'lib/websocket/driver/client.rb', line 44 def proxy(origin, = {}) Proxy.new(self, origin, ) end |
#start ⇒ Object
48 49 50 51 52 53 |
# File 'lib/websocket/driver/client.rb', line 48 def start return false unless @ready_state == -1 @socket.write(Driver.encode(handshake_request, :binary)) @ready_state = 0 true end |
#version ⇒ Object
40 41 42 |
# File 'lib/websocket/driver/client.rb', line 40 def version 'hybi-13' end |