Class: Protocol::WebSocket::CloseFrame
- Defined in:
- lib/protocol/websocket/close_frame.rb
Overview
Represents a close frame that is sent or received by a WebSocket connection.
Constant Summary collapse
- OPCODE =
0x8- FORMAT =
"na*"
Constants inherited from Frame
Frame::RESERVED, Frame::RSV1, Frame::RSV2, Frame::RSV3
Instance Attribute Summary
Attributes inherited from Frame
#finished, #flags, #length, #mask, #opcode, #payload
Instance Method Summary collapse
-
#apply(connection) ⇒ Object
Apply this frame to the specified connection.
-
#pack(code = nil, reason = nil) ⇒ Object
Pack a close code and reason into the frame data.
-
#reply(code = Error::NO_ERROR, reason = "") ⇒ Object
Generate a suitable reply.
-
#unpack ⇒ Object
Unpack the frame data into a close code and reason.
Methods inherited from Frame
#<=>, #continued?, #control?, #data?, #finished?, #flag?, #initialize, parse_header, read, #to_ary, #write
Constructor Details
This class inherits a constructor from Protocol::WebSocket::Frame
Instance Method Details
#apply(connection) ⇒ Object
Apply this frame to the specified connection.
71 72 73 |
# File 'lib/protocol/websocket/close_frame.rb', line 71 def apply(connection) connection.receive_close(self) end |
#pack(code = nil, reason = nil) ⇒ Object
Pack a close code and reason into the frame data. If code is missing, reason is ignored.
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/protocol/websocket/close_frame.rb', line 50 def pack(code = nil, reason = nil) if code if reason and reason.encoding != Encoding::UTF_8 reason = reason.encode(Encoding::UTF_8) end super([code, reason].pack(FORMAT)) else super() end end |
#reply(code = Error::NO_ERROR, reason = "") ⇒ Object
Generate a suitable reply.
64 65 66 67 68 |
# File 'lib/protocol/websocket/close_frame.rb', line 64 def reply(code = Error::NO_ERROR, reason = "") frame = CloseFrame.new frame.pack(code, reason) return frame end |
#unpack ⇒ Object
Unpack the frame data into a close code and reason.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/protocol/websocket/close_frame.rb', line 18 def unpack data = super case data.length when 0 [nil, nil] when 1 raise ProtocolError, "Invalid close frame length!" else code, reason = *data.unpack(FORMAT) case code when 0 .. 999, 1005 .. 1006, 1015, 5000 .. 0xFFFF raise ProtocolError, "Invalid close code!" when 1004, 1016 .. 2999 raise ProtocolError, "Reserved close code!" end reason.force_encoding(Encoding::UTF_8) unless reason.valid_encoding? raise ProtocolError, "Invalid UTF-8 in close reason!" end [code, reason] end end |