Class: WebSocket::Frame::Data

Inherits:
String
  • Object
show all
Defined in:
lib/websocket/frame/data.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Data

Returns a new instance of Data.



5
6
7
# File 'lib/websocket/frame/data.rb', line 5

def initialize(*args)
  super *args.each { |arg| arg.force_encoding('ASCII-8BIT') if respond_to?(:force_encoding) }
end

Instance Method Details

#<<(*args) ⇒ Object



9
10
11
# File 'lib/websocket/frame/data.rb', line 9

def <<(*args)
  super *args.each { |arg| arg.force_encoding('ASCII-8BIT') if respond_to?(:force_encoding) }
end

#getbyte(index) ⇒ Object



30
31
32
# File 'lib/websocket/frame/data.rb', line 30

def getbyte(index)
  self[index]
end

#getbytes(start_index, count) ⇒ Object



22
23
24
25
26
# File 'lib/websocket/frame/data.rb', line 22

def getbytes(start_index, count)
  data = self[start_index, count]
  data = mask(data.bytes.to_a, @masking_key).pack('C*') if @masking_key
  data
end

#mask(payload, mask) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/websocket/frame/data.rb', line 35

def mask(payload, mask)
  return mask_native(payload, mask) if respond_to?(:mask_native)
  result = []
  payload.each_with_index do |byte, i|
    result[i] = byte ^ mask[i % 4]
  end
  result
end

#set_maskObject



13
14
15
16
# File 'lib/websocket/frame/data.rb', line 13

def set_mask
  raise "Too short" if bytesize < 4 # TODO - change
  @masking_key = self[0..3].bytes.to_a
end

#unset_maskObject



18
19
20
# File 'lib/websocket/frame/data.rb', line 18

def unset_mask
  @masking_key = nil
end