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
                # Sec-WebSocket-Origin: http://example.com
                # Sec-WebSocket-Version: 13
                # Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==

# Parse server response
@handshake << "HTTP/1.1 101 Switching Protocols\\r\nUpgrade: websocket\\r\nConnection: Upgrade\\r\nSec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\\r\n\\r\n"

# All data received?
@handshake.finished?

# No parsing errors?
@handshake.valid?

Instance Attribute Summary

Attributes inherited from Base

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

Instance Method Summary collapse

Methods inherited from Base

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

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

  • :version (Integer)

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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/websocket/handshake/client.rb', line 52

def initialize(args = {})
  super

  @version = args[:version] || DEFAULT_VERSION
  @origin = args[:origin]

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

  @secure = args[:secure] if args[:secure]
  @host   = args[:host]   if args[:host]
  @port   = args[:port]   if args[:port]
  @path   = args[:path]   if args[:path]
  @query  = args[:query]  if args[:query]

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

  set_error(:no_host_provided) unless @host

  include_version
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 << "HTTP/1.1 101 Switching Protocols\nUpgrade: websocket\nConnection: Upgrade\nSec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\n\n"

Parameters:

  • data (String)

    Data to add



92
93
94
95
96
97
# File 'lib/websocket/handshake/client.rb', line 92

def <<(data)
  @data << data
  if parse_data

  end
end

#should_respond?Boolean

Should send content to server after finished parsing?

Returns:

  • (Boolean)

    false



101
102
103
# File 'lib/websocket/handshake/client.rb', line 101

def should_respond?
  false
end