Class: WebSocket::Handshake::Base Abstract

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Base

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



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

def initialize(args = {})
  @state = :new

  @data = ""
  @headers = {}
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



6
7
8
# File 'lib/websocket/handshake/base.rb', line 6

def error
  @error
end

#hostObject (readonly)

Returns the value of attribute host.



6
7
8
# File 'lib/websocket/handshake/base.rb', line 6

def host
  @host
end

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/websocket/handshake/base.rb', line 6

def path
  @path
end

#portObject (readonly)

Returns the value of attribute port.



6
7
8
# File 'lib/websocket/handshake/base.rb', line 6

def port
  @port
end

#queryObject (readonly)

Returns the value of attribute query.



6
7
8
# File 'lib/websocket/handshake/base.rb', line 6

def query
  @query
end

#secureObject (readonly)

Returns the value of attribute secure.



6
7
8
# File 'lib/websocket/handshake/base.rb', line 6

def secure
  @secure
end

#stateObject (readonly)

Returns the value of attribute state.



6
7
8
# File 'lib/websocket/handshake/base.rb', line 6

def state
  @state
end

#versionObject (readonly)

Returns the value of attribute version.



6
7
8
# File 'lib/websocket/handshake/base.rb', line 6

def version
  @version
end

Instance Method Details

#<<(data) ⇒ Object

This method is abstract.

Add data to handshake

Raises:

  • (NotImplementedError)


18
19
20
# File 'lib/websocket/handshake/base.rb', line 18

def <<(data)
  raise NotImplementedError
end

#finished?Boolena

Is parsing of data finished?

Returns:

  • (Boolena)

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



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

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

#inspectObject

Recreate inspect as #to_s was overwritten



29
30
31
32
33
# File 'lib/websocket/handshake/base.rb', line 29

def inspect
  vars = self.instance_variables.map{|v| "#{v}=#{instance_variable_get(v).inspect}"}.join(", ")
  insp = "#{self.class}:0x%08x" % (self.__id__ * 2)
  "<#{insp} #{vars}>"
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



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

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

#should_respond?Boolean

This method is abstract.

Should send data after parsing is finished?

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


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

def should_respond?
  raise NotImplementedError
end

#to_sString

Return textual representation of handshake request or response

Returns:

  • (String)

    text of response



24
25
26
# File 'lib/websocket/handshake/base.rb', line 24

def to_s
  ""
end

#uriString

URI of request.

Examples:

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

Returns:

  • (String)

    Full URI with protocol



62
63
64
65
66
67
68
69
# File 'lib/websocket/handshake/base.rb', line 62

def uri
  uri =  secure ? "wss://" : "ws://"
  uri << host
  uri << ":#{port}" if 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



43
44
45
# File 'lib/websocket/handshake/base.rb', line 43

def valid?
  finished? && @error == nil
end