Class: WebSocket::Handshake::Base Abstract

Inherits:
Object
  • Object
show all
Includes:
ExceptionHandler, NiceInspect
Defined in:
lib/websocket/handshake/base.rb

Overview

This class is abstract.

Subclass and override to implement custom handshakes

Direct Known Subclasses

Client, Server

Instance Attribute Summary collapse

Attributes included from ExceptionHandler

#error

Instance Method Summary collapse

Methods included from NiceInspect

#inspect

Methods included from ExceptionHandler

included

Constructor Details

#initialize(args = {}) ⇒ Base

Initialize new WebSocket Handshake and set it's state to :new


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/websocket/handshake/base.rb', line 15

def initialize(args = {})
  args.each do |k, v|
    value = begin
      v.dup
    rescue TypeError
      v
    end
    instance_variable_set("@#{k}", value)
  end

  @state = :new
  @handler = nil

  @data = String.new('')
  @headers ||= {}
  @protocols ||= []
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers


10
11
12
# File 'lib/websocket/handshake/base.rb', line 10

def headers
  @headers
end

#hostObject (readonly)

Returns the value of attribute host


10
11
12
# File 'lib/websocket/handshake/base.rb', line 10

def host
  @host
end

#pathObject (readonly)

Returns the value of attribute path


10
11
12
# File 'lib/websocket/handshake/base.rb', line 10

def path
  @path
end

#portObject (readonly)

Returns the value of attribute port


10
11
12
# File 'lib/websocket/handshake/base.rb', line 10

def port
  @port
end

#protocolsObject (readonly)

Returns the value of attribute protocols


10
11
12
# File 'lib/websocket/handshake/base.rb', line 10

def protocols
  @protocols
end

#queryObject (readonly)

Returns the value of attribute query


10
11
12
# File 'lib/websocket/handshake/base.rb', line 10

def query
  @query
end

#secureObject (readonly)

Returns the value of attribute secure


10
11
12
# File 'lib/websocket/handshake/base.rb', line 10

def secure
  @secure
end

#stateObject (readonly)

Returns the value of attribute state


10
11
12
# File 'lib/websocket/handshake/base.rb', line 10

def state
  @state
end

#versionObject (readonly)

Returns the value of attribute version


10
11
12
# File 'lib/websocket/handshake/base.rb', line 10

def version
  @version
end

Instance Method Details

#<<(data) ⇒ Object

This method is abstract.

Add data to handshake


34
35
36
# File 'lib/websocket/handshake/base.rb', line 34

def <<(data)
  @data << data
end

#finished?Boolena

Is parsing of data finished?


47
48
49
# File 'lib/websocket/handshake/base.rb', line 47

def finished?
  @state == :finished || @state == :error
end

#leftoversString

Data left from parsing. Sometimes data that doesn't belong to handshake are added - use this method to retrieve them.


65
66
67
# File 'lib/websocket/handshake/base.rb', line 65

def leftovers
  (@leftovers.to_s.split("\n", reserved_leftover_lines + 1)[reserved_leftover_lines] || '').strip
end

#should_respond?Boolean

This method is abstract.

Should send data after parsing is finished?

Raises:

  • (NotImplementedError)

59
60
61
# File 'lib/websocket/handshake/base.rb', line 59

def should_respond?
  raise NotImplementedError
end

#to_sString

Return textual representation of handshake request or response


40
41
42
# File 'lib/websocket/handshake/base.rb', line 40

def to_s
  @handler ? @handler.to_s : ''
end

#uriString

URI of request.

Examples:

@handshake.uri #=> "ws://example.com/path?query=true"

73
74
75
76
77
78
79
80
# File 'lib/websocket/handshake/base.rb', line 73

def uri
  uri =  String.new(secure ? 'wss://' : 'ws://')
  uri << host
  uri << ":#{port}" if port
  uri << path
  uri << "?#{query}" if query
  uri
end

#valid?Boolean

Is parsed data valid?


53
54
55
# File 'lib/websocket/handshake/base.rb', line 53

def valid?
  finished? && @error.nil? && @handler && @handler.valid?
end