class WebSocketRb::Wrapper::FrameBase

Class defines base frame structure.

Constants

BINARY
CLOSE
CONTINUATION

Constants represents type of frame

PING
PONG
TEXT

Attributes

fin[RW]
mask[RW]
masking_key[RW]
opcode[RW]
payload_data[RW]
payload_len[RW]
rsv1[RW]
rsv2[RW]
rsv3[RW]

Public Instance Methods

destination() click to toggle source

Read destination from payload data

# File lib/web_socket_rb/wrapper/frame_base.rb, line 35
def destination
  json_payload_data = JSON.parse(payload_data)
  json_payload_data.is_a?(Hash) ? json_payload_data['destination'] : nil
end
message() click to toggle source

Read message from payload data

# File lib/web_socket_rb/wrapper/frame_base.rb, line 41
def message
  json_payload_data = JSON.parse(payload_data)
  json_payload_data.is_a?(Hash) ? json_payload_data['message'] : nil
end
to_bytes() click to toggle source

Method converts frame to bytes representation.

# File lib/web_socket_rb/wrapper/frame_base.rb, line 17
def to_bytes
  bytes = ''
  bytes << fin_rsv_opcode_to_byte
  bytes << mask_payload_length_to_byte
  bytes << ext_payload_len_to_byte
  bytes << masking_key_to_byte
  bytes << payload_data_to_byte
  bytes
end

Protected Instance Methods

ext_payload_len_to_byte() click to toggle source
# File lib/web_socket_rb/wrapper/frame_base.rb, line 62
def ext_payload_len_to_byte
  case payload_len
  when 0..125
    ''
  when 126..65_535
    [payload_len].pack('S>')
  when 65_536..18_446_744_073_709_551_615
    [payload_len].pack('Q>')
  end
end
fin_rsv_opcode_to_byte() click to toggle source
# File lib/web_socket_rb/wrapper/frame_base.rb, line 90
def fin_rsv_opcode_to_byte
  byte = 0
  byte |= 1 if fin

  byte <<= 1
  byte |= 1 if rsv1

  byte <<= 1
  byte |= 1 if rsv2

  byte <<= 1
  byte |= 1 if rsv3

  byte <<= 4
  byte |= opcode

  byte.chr
end
mask_payload_length_to_byte() click to toggle source
# File lib/web_socket_rb/wrapper/frame_base.rb, line 73
def mask_payload_length_to_byte
  byte = 0
  byte |= 1 if mask
  byte <<= 7

  case payload_len
  when 0..125
    byte |= payload_len
  when 126..65_535
    byte |= 126
  when 65_536..18_446_744_073_709_551_615
    byte |= 127
  end

  byte.chr
end
masking_key_to_byte() click to toggle source
# File lib/web_socket_rb/wrapper/frame_base.rb, line 58
def masking_key_to_byte
  mask && masking_key ? masking_key.bytes.pack('C*') : ''
end
payload_data_to_byte() click to toggle source
# File lib/web_socket_rb/wrapper/frame_base.rb, line 48
def payload_data_to_byte
  if mask
    payload_data.bytes.each_slice(4).map do |slice|
      slice.zip(masking_key.bytes).map { |a, b| a ^ b }
    end.flatten.pack('C*')
  else
    payload_data.bytes.pack('C*')
  end
end