Class: Protocol::WebSocket::Framer
- Inherits:
-
Object
- Object
- Protocol::WebSocket::Framer
- Defined in:
- lib/protocol/websocket/framer.rb
Overview
Wraps an underlying Async::IO::Stream for reading and writing binary data into structured frames.
Instance Method Summary collapse
- #close ⇒ Object
- #flush ⇒ Object
-
#initialize(stream, frames = FRAMES) ⇒ Framer
constructor
A new instance of Framer.
-
#read_frame(maximum_frame_size = MAXIMUM_ALLOWED_FRAME_SIZE) ⇒ Object
Read a frame from the underlying stream.
- #read_header ⇒ Object
- #write_frame(frame) ⇒ Object
Constructor Details
Instance Method Details
#close ⇒ Object
37 38 39 |
# File 'lib/protocol/websocket/framer.rb', line 37 def close @stream.close end |
#flush ⇒ Object
41 42 43 |
# File 'lib/protocol/websocket/framer.rb', line 41 def flush @stream.flush end |
#read_frame(maximum_frame_size = MAXIMUM_ALLOWED_FRAME_SIZE) ⇒ Object
Read a frame from the underlying stream.
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/protocol/websocket/framer.rb', line 47 def read_frame(maximum_frame_size = MAXIMUM_ALLOWED_FRAME_SIZE) # Read the header: finished, flags, opcode = read_header # Read the frame: klass = @frames[opcode] || Frame frame = klass.read(finished, flags, opcode, @stream, maximum_frame_size) return frame end |
#read_header ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/protocol/websocket/framer.rb', line 62 def read_header if buffer = @stream.read(1) and buffer.bytesize == 1 return Frame.parse_header(buffer) end raise EOFError, "Could not read frame header!" end |
#write_frame(frame) ⇒ Object
58 59 60 |
# File 'lib/protocol/websocket/framer.rb', line 58 def write_frame(frame) frame.write(@stream) end |