Parent

LibWebSocket::Frame

Construct or parse a WebSocket frame.

SYNOPSIS

# Create frame
frame = LibWebSocket::Frame.new('123')
frame.to_s # \x00123\xff

# Parse frames
frame = LibWebSocket::Frame.new
frame.append("123\x00foo\xff56\x00bar\xff789")
frame.next # =>  foo
frame.next # =>  bar

Public Class Methods

new(buffer = nil) click to toggle source
# File lib/libwebsocket/frame.rb, line 17
def initialize(buffer = nil)
  @buffer = buffer || ''
end

Public Instance Methods

append(string = nil) click to toggle source

Append a frame chunk. @example

frame.append("\x00foo")
frame.append("bar\xff")
# File lib/libwebsocket/frame.rb, line 30
def append(string = nil)
  return unless string.is_a?(String)

  @buffer += string
  @buffer.force_encoding('ASCII-8BIT') if @buffer.respond_to?(:force_encoding)

  return self
end
new(buffer = nil) click to toggle source

Create new frame without modification of current

# File lib/libwebsocket/frame.rb, line 22
def new(buffer = nil)
  self.class.new(buffer)
end
next() click to toggle source

Return the next frame. @example

frame.append("\x00foo")
frame.append("\xff\x00bar\xff")

frame.next; # =>  foo
frame.next; # =>  bar
# File lib/libwebsocket/frame.rb, line 46
def next
  return unless @buffer.slice!(/^[^\x00]*\x00(.*?)\xff/)

  string = $1
  string.force_encoding('UTF-8') if string.respond_to?(:force_encoding)

  return string
end
to_s() click to toggle source

Construct a WebSocket frame. @example

frame = LibWebSocket::Frame.new('foo')
frame.to_s # => \x00foo\xff
# File lib/libwebsocket/frame.rb, line 59
def to_s
  ary = ["\x00", @buffer.dup, "\xff"]

  ary.collect{ |s| s.force_encoding('UTF-8') if s.respond_to?(:force_encoding) }

  return ary.join
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.