Class: WebSocket::Frame::Handler::Handler75
- Inherits:
-
Base
- Object
- Base
- WebSocket::Frame::Handler::Handler75
show all
- Defined in:
- lib/websocket/frame/handler/handler75.rb
Instance Method Summary
collapse
Methods inherited from Base
#initialize
Instance Method Details
#decode_frame ⇒ Object
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/websocket/frame/handler/handler75.rb', line 26
def decode_frame
return if @frame.data.size.zero?
pointer = 0
frame_type = @frame.data.getbyte(pointer)
pointer += 1
if (frame_type & 0x80) == 0x80
length = 0
loop do
return unless @frame.data.getbyte(pointer)
b = @frame.data.getbyte(pointer)
pointer += 1
b_v = b & 0x7F
length = length * 128 + b_v
break unless (b & 0x80) == 0x80
end
raise WebSocket::Error::Frame::TooLong if length > ::WebSocket.max_frame_size
unless @frame.data.getbyte(pointer + length - 1).nil?
@frame.instance_variable_set '@data', @frame.data[(pointer + length)..-1]
if length.zero?
@frame.class.new(version: @frame.version, type: :close, decoded: true)
end
end
else
raise WebSocket::Error::Frame::Invalid if @frame.data.getbyte(0) != 0x00
raise WebSocket::Error::Frame::TooLong if @frame.data.size > ::WebSocket.max_frame_size
msg = @frame.data.slice!(/\A\x00[^\xff]*\xff/)
if msg
msg.gsub!(/\A\x00|\xff\z/, '')
msg.force_encoding('UTF-8')
@frame.class.new(version: @frame.version, type: :text, data: msg, decoded: true)
end
end
end
|
#encode_frame ⇒ Object
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/websocket/frame/handler/handler75.rb', line 14
def encode_frame
case @frame.type
when :close then "\xff\x00"
when :text then
ary = ["\x00", @frame.data, "\xff"]
ary.map { |s| s.encode('UTF-8', 'UTF-8', invalid: :replace) }
ary.join
else raise WebSocket::Error::Frame::UnknownFrameType
end
end
|
#supported_frames ⇒ Object
9
10
11
|
# File 'lib/websocket/frame/handler/handler75.rb', line 9
def supported_frames
%i[text close]
end
|