Class: WebSocket::Handshake::Client

Inherits:
Base
  • Object
show all
Defined in:
lib/websocket/handshake/client.rb

Overview

Construct or parse a client WebSocket handshake.

Examples:

@handshake = WebSocket::Handshake::Client.new(url: 'ws://example.com')

# Create request
@handshake.to_s # GET /demo HTTP/1.1
                # Upgrade: websocket
                # Connection: Upgrade
                # Host: example.com
                # Origin: http://example.com
                # Sec-WebSocket-Version: 13
                # Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==

# Parse server response
@handshake << <<EOF
HTTP/1.1 101 Switching Protocols\r
Upgrade: websocket\r
Connection: Upgrade\r
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r
\r
EOF

# All data received?
@handshake.finished?

# No parsing errors?
@handshake.valid?

Instance Attribute Summary collapse

Attributes inherited from Base

#host, #path, #port, #protocols, #query, #secure, #state, #version

Attributes included from ExceptionHandler

#error

Instance Method Summary collapse

Methods inherited from Base

#finished?, #leftovers, #to_s, #uri, #valid?

Methods included from NiceInspect

#inspect

Methods included from ExceptionHandler

included

Constructor Details

#initialize(args = {}) ⇒ Client

Initialize new WebSocket Client

Examples:

Websocket::Handshake::Client.new(url: "ws://example.com/path?query=true")

Parameters:

  • args (Hash) (defaults to: {})

    Arguments for client

Options Hash (args):

  • :host (String)

    Host of request. Required if no :url param was provided.

  • :origin (String)

    Origin of request. Optional, should be used mostly by browsers. Default: nil

  • :path (String)

    Path of request. Should start with '/'. Default: '/'

  • :port (Integer)

    Port of request. Default: nil

  • :query. (String)

    Query for request. Should be in format “aaa=bbb&ccc=ddd”

  • :secure (Boolean)

    Defines protocol to use. If true then wss://, otherwise ws://. This option will not change default port - it should be handled by programmer.

  • :url (String)

    URL of request. Must by in format like ws://example.com/path?query=true. Every part of this url will be overriden by more specific arguments.

  • :uri (String)

    Alias to :url

  • :protocols (Array<String>)

    An array of supported sub-protocols

  • :version (Integer)

    Version of WebSocket to use. Default: 13 (this is version from RFC)

  • :headers (Hash)

    HTTP headers to use in the handshake

Raises:



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/websocket/handshake/client.rb', line 57

def initialize(args = {})
  super

  if @url || @uri
    uri = URI.parse(@url || @uri)
    @secure ||= (uri.scheme == 'wss')
    @host ||= uri.host
    @port ||= uri.port
    @path ||= uri.path
    @query ||= uri.query
  end

  @path = '/' if @path.nil? || @path.empty?
  @version ||= DEFAULT_VERSION

  raise WebSocket::Error::Handshake::NoHostProvided unless @host

  include_version
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers



37
38
39
# File 'lib/websocket/handshake/client.rb', line 37

def headers
  @headers
end

#originObject (readonly)

Returns the value of attribute origin



37
38
39
# File 'lib/websocket/handshake/client.rb', line 37

def origin
  @origin
end

Instance Method Details

#<<(data) ⇒ Object

Add text of response from Server. This method will parse content immediately and update state and error(if neccessary)

Examples:

@handshake << <<EOF
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=

EOF

Parameters:

  • data (String)

    Data to add



90
91
92
93
# File 'lib/websocket/handshake/client.rb', line 90

def <<(data)
  super
  parse_data
end

#should_respond?Boolean

Should send content to server after finished parsing?

Returns:

  • (Boolean)

    false



98
99
100
# File 'lib/websocket/handshake/client.rb', line 98

def should_respond?
  false
end