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

#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

#default_portObject

Return default port for protocol (80 for ws, 443 for wss)



70
71
72
# File 'lib/websocket/handshake/base.rb', line 70

def default_port
  secure ? 443 : 80
end

#default_port?Boolean

Check if provided port is a default one

Returns:

  • (Boolean)


75
76
77
# File 'lib/websocket/handshake/base.rb', line 75

def default_port?
  port == default_port
end

#finished?Boolena

Is parsing of data finished?

Returns:

  • (Boolena)

    True if request was completely parsed or error occured. False otherwise



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.

Returns:

  • (String)

    String if some data are available. Nil otherwise



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

#portObject



79
80
81
# File 'lib/websocket/handshake/base.rb', line 79

def port
  @port || default_port
end

#should_respond?Boolean

This method is abstract.

Should send data after parsing is finished?

Returns:

  • (Boolean)

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

Returns:

  • (String)

    text of 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"

Returns:

  • (String)

    Full URI with protocol



87
88
89
90
91
92
93
94
# File 'lib/websocket/handshake/base.rb', line 87

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

#valid?Boolean

Is parsed data valid?

Returns:

  • (Boolean)

    False if some errors occured. Reason for error could be found in error method



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

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