Class: Lich::Common::WebSocket::Frame::Reader

Inherits:
Object
  • Object
show all
Defined in:
documented/common/websocket/frame.rb

Overview

Incremental frame decoder. Feed it raw bytes as they arrive off the socket; it returns whatever complete, reassembled messages became available, and holds on to any trailing partial frame until the next call.

Defined Under Namespace

Classes: RawFrame

Instance Method Summary collapse

Constructor Details

#initialize ⇒ void

Initializes the frame reader with an empty buffer and no fragment in progress.



141
142
143
144
# File 'documented/common/websocket/frame.rb', line 141

def initialize
  @buffer = +"".b
  @fragment = nil # [opcode, +payload]
end

Instance Method Details

#feed(bytes) ⇒ Array<Message>

Returns zero or more complete messages.

Parameters:

  • bytes (String) —

    newly-received raw bytes

Returns:

  • (Array<Message>) —

    zero or more complete messages

Raises:



149
150
151
152
153
154
155
156
157
158
159
# File 'documented/common/websocket/frame.rb', line 149

def feed(bytes)
  @buffer << bytes
  messages = []
  while (frame = take_frame!)
    messages.concat(handle_frame(frame))
  end
  messages
rescue ProtocolError => e
  e.decoded_messages = messages
  raise
end