Class: Protocol::WebSocket::CloseFrame

Inherits:
Frame
  • Object
show all
Defined in:
lib/protocol/websocket/close_frame.rb

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

Methods inherited from Frame

#<=>, #continued?, #control?, #data?, #finished?, #initialize, parse_header, read, #to_ary, #write

Constructor Details

This class inherits a constructor from Protocol::WebSocket::Frame

Instance Method Details

#apply(connection) ⇒ Object



56
57
58
# File 'lib/protocol/websocket/close_frame.rb', line 56

def apply(connection)
	connection.receive_close(self)
end

#pack(code = nil, reason = nil) ⇒ Object

If code is missing, reason is ignored.



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/protocol/websocket/close_frame.rb', line 44

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

#unpackObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/protocol/websocket/close_frame.rb', line 15

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