Class: Protocol::WebSocket::Frame
- Inherits:
-
Object
- Object
- Protocol::WebSocket::Frame
- Includes:
- Comparable
- Defined in:
- lib/protocol/websocket/frame.rb
Overview
Represents a single WebSocket frame as defined by RFC 6455.
Direct Known Subclasses
BinaryFrame, CloseFrame, ContinuationFrame, PingFrame, PongFrame, TextFrame
Constant Summary collapse
Instance Attribute Summary collapse
-
#finished ⇒ Object
The generic frame header uses the following binary representation:.
-
#flags ⇒ Object
Returns the value of attribute flags.
-
#mask ⇒ Object
Returns the value of attribute mask.
-
#opcode ⇒ Object
Returns the value of attribute opcode.
-
#payload ⇒ Object
Returns the value of attribute payload.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Compare this frame to another frame by their array representation.
-
#apply(connection) ⇒ Object
Apply this frame to the connection by dispatching it to the appropriate handler.
-
#continued? ⇒ Boolean
Check whether this frame is a continuation fragment (FIN bit not set).
-
#control? ⇒ Boolean
Check whether this is a control frame (opcode has bit 3 set).
- #data? ⇒ Boolean
-
#finished? ⇒ Boolean
Check whether this is the final frame in a message.
-
#flag?(value) ⇒ Boolean
Check whether the specified RSV flag bit is set on this frame.
-
#initialize(finished = true, payload = nil, flags: 0, opcode: self.class::OPCODE, mask: false) ⇒ Frame
constructor
A new instance of Frame.
-
#length ⇒ Object
The byte length of the payload.
-
#pack(data = "") ⇒ Object
Pack the given data into this frame’s payload, applying masking if configured.
-
#to_ary ⇒ Object
Convert this frame to an array of its fields for comparison or inspection.
-
#unpack ⇒ Object
Unpack the raw payload, removing masking if present.
Constructor Details
#initialize(finished = true, payload = nil, flags: 0, opcode: self.class::OPCODE, mask: false) ⇒ Frame
Returns a new instance of Frame.
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/protocol/websocket/frame.rb', line 26 def initialize(finished = true, payload = nil, flags: 0, opcode: self.class::OPCODE, mask: false) if mask == true mask = SecureRandom.bytes(4) end @finished = finished @flags = flags @opcode = opcode @mask = mask @payload = payload end |
Instance Attribute Details
#finished ⇒ Object
The generic frame header uses the following binary representation:
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
-------------------------——————————-+ |F|R|R|R| opcode|M| Payload len | Extended payload length | |I|S|S|S| (4) |A| (7) | (16/64) | |N|V|V|V| |S| | (if payload len==126/127) | | |1|2|3| |K| | | ------------------------- - - - - - - - - - - - - - - - + | Extended payload length continued, if payload len == 127 | + - - - - - - - - - - - - - - - ------------------------------- | |Masking-key, if MASK set to 1 | -------------------------------——————————-+ | Masking-key (continued) | Payload Data | ——————————– - - - - - - - - - - - - - - - : Payload Data continued … : + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + | Payload Data continued … | ---------------------------------------------------------------
102 103 104 |
# File 'lib/protocol/websocket/frame.rb', line 102 def finished @finished end |
#flags ⇒ Object
Returns the value of attribute flags.
103 104 105 |
# File 'lib/protocol/websocket/frame.rb', line 103 def flags @flags end |
#mask ⇒ Object
Returns the value of attribute mask.
105 106 107 |
# File 'lib/protocol/websocket/frame.rb', line 105 def mask @mask end |
#opcode ⇒ Object
Returns the value of attribute opcode.
104 105 106 |
# File 'lib/protocol/websocket/frame.rb', line 104 def opcode @opcode end |
#payload ⇒ Object
Returns the value of attribute payload.
106 107 108 |
# File 'lib/protocol/websocket/frame.rb', line 106 def payload @payload end |
Instance Method Details
#<=>(other) ⇒ Object
Compare this frame to another frame by their array representation.
48 49 50 |
# File 'lib/protocol/websocket/frame.rb', line 48 def <=> other to_ary <=> other.to_ary end |
#apply(connection) ⇒ Object
Apply this frame to the connection by dispatching it to the appropriate handler.
167 168 169 |
# File 'lib/protocol/websocket/frame.rb', line 167 def apply(connection) connection.receive_frame(self) end |
#continued? ⇒ Boolean
Check whether this frame is a continuation fragment (FIN bit not set).
77 78 79 |
# File 'lib/protocol/websocket/frame.rb', line 77 def continued? @finished == false end |
#control? ⇒ Boolean
Check whether this is a control frame (opcode has bit 3 set).
60 61 62 |
# File 'lib/protocol/websocket/frame.rb', line 60 def control? @opcode & 0x8 != 0 end |
#data? ⇒ Boolean
65 66 67 |
# File 'lib/protocol/websocket/frame.rb', line 65 def data? false end |
#finished? ⇒ Boolean
Check whether this is the final frame in a message.
71 72 73 |
# File 'lib/protocol/websocket/frame.rb', line 71 def finished? @finished == true end |
#flag?(value) ⇒ Boolean
Check whether the specified RSV flag bit is set on this frame.
41 42 43 |
# File 'lib/protocol/websocket/frame.rb', line 41 def flag?(value) @flags & value != 0 end |
#length ⇒ Object
The byte length of the payload.
110 111 112 |
# File 'lib/protocol/websocket/frame.rb', line 110 def length @payload&.bytesize end |
#pack(data = "") ⇒ Object
Pack the given data into this frame’s payload, applying masking if configured.
141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/protocol/websocket/frame.rb', line 141 def pack(data = "") if data.bytesize.bit_length > 63 raise ProtocolError, "Frame length #{data.bytesize} bigger than allowed maximum!" end if @mask @payload = mask_xor(data, @mask) else @payload = data end return self end |
#to_ary ⇒ Object
Convert this frame to an array of its fields for comparison or inspection.
54 55 56 |
# File 'lib/protocol/websocket/frame.rb', line 54 def to_ary [@finished, @flags, @opcode, @mask, @payload] end |
#unpack ⇒ Object
Unpack the raw payload, removing masking if present.
157 158 159 160 161 162 163 |
# File 'lib/protocol/websocket/frame.rb', line 157 def unpack if @mask and !@payload.empty? return mask_xor(@payload, @mask) else return @payload end end |