Class: WebSocket::Handshake::Base Abstract
- Inherits:
-
Object
- Object
- WebSocket::Handshake::Base
- Includes:
- ExceptionHandler, NiceInspect
- Defined in:
- lib/websocket/handshake/base.rb
Overview
Subclass and override to implement custom handshakes
Instance Attribute Summary collapse
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#protocols ⇒ Object
readonly
Returns the value of attribute protocols.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
-
#secure ⇒ Object
readonly
Returns the value of attribute secure.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Attributes included from ExceptionHandler
Instance Method Summary collapse
- #<<(data) ⇒ Object abstract
-
#default_port ⇒ Object
Return default port for protocol (80 for ws, 443 for wss).
-
#default_port? ⇒ Boolean
Check if provided port is a default one.
-
#finished? ⇒ Boolena
Is parsing of data finished?.
-
#initialize(args = {}) ⇒ Base
constructor
Initialize new WebSocket Handshake and set it’s state to :new.
-
#leftovers ⇒ String
Data left from parsing.
- #port ⇒ Object
- #should_respond? ⇒ Boolean abstract
-
#to_s ⇒ String
Return textual representation of handshake request or response.
-
#uri ⇒ String
URI of request.
-
#valid? ⇒ Boolean
Is parsed data valid?.
Methods included from NiceInspect
Methods included from ExceptionHandler
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
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
10 11 12 |
# File 'lib/websocket/handshake/base.rb', line 10 def headers @headers end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
10 11 12 |
# File 'lib/websocket/handshake/base.rb', line 10 def host @host end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
10 11 12 |
# File 'lib/websocket/handshake/base.rb', line 10 def path @path end |
#protocols ⇒ Object (readonly)
Returns the value of attribute protocols.
10 11 12 |
# File 'lib/websocket/handshake/base.rb', line 10 def protocols @protocols end |
#query ⇒ Object (readonly)
Returns the value of attribute query.
10 11 12 |
# File 'lib/websocket/handshake/base.rb', line 10 def query @query end |
#secure ⇒ Object (readonly)
Returns the value of attribute secure.
10 11 12 |
# File 'lib/websocket/handshake/base.rb', line 10 def secure @secure end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
10 11 12 |
# File 'lib/websocket/handshake/base.rb', line 10 def state @state end |
#version ⇒ Object (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
Add data to handshake
34 35 36 |
# File 'lib/websocket/handshake/base.rb', line 34 def <<(data) @data << data end |
#default_port ⇒ Object
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
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?
47 48 49 |
# File 'lib/websocket/handshake/base.rb', line 47 def finished? @state == :finished || @state == :error end |
#leftovers ⇒ String
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 |
#port ⇒ Object
79 80 81 |
# File 'lib/websocket/handshake/base.rb', line 79 def port @port || default_port end |
#should_respond? ⇒ Boolean
Should send data after parsing is finished?
59 60 61 |
# File 'lib/websocket/handshake/base.rb', line 59 def should_respond? raise NotImplementedError end |
#to_s ⇒ String
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 |
#uri ⇒ String
URI of request.
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?
53 54 55 |
# File 'lib/websocket/handshake/base.rb', line 53 def valid? finished? && @error.nil? && @handler && @handler.valid? end |