Class: Protocol::WebSocket::Framer

Inherits:
Object
  • Object
show all
Defined in:
lib/protocol/websocket/framer.rb

Instance Method Summary collapse

Constructor Details

#initialize(stream, frames = FRAMES) ⇒ Framer

Returns a new instance of Framer.



45
46
47
48
# File 'lib/protocol/websocket/framer.rb', line 45

def initialize(stream, frames = FRAMES)
	@stream = stream
	@frames = frames
end

Instance Method Details

#closeObject



50
51
52
# File 'lib/protocol/websocket/framer.rb', line 50

def close
	@stream.close
end

#flushObject



54
55
56
# File 'lib/protocol/websocket/framer.rb', line 54

def flush
	@stream.flush
end

#read_frame(maximum_frame_size = MAXIMUM_ALLOWED_FRAME_SIZE) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/protocol/websocket/framer.rb', line 58

def read_frame(maximum_frame_size = MAXIMUM_ALLOWED_FRAME_SIZE)
	# Read the header:
	finished, opcode = read_header
	
	# Read the frame:
	klass = @frames[opcode] || Frame
	frame = klass.read(finished, opcode, @stream, maximum_frame_size)
	
	return frame
end

#read_headerObject

Raises:

  • (EOFError)


73
74
75
76
77
78
79
# File 'lib/protocol/websocket/framer.rb', line 73

def read_header
	if buffer = @stream.read(1)
		return Frame.parse_header(buffer)
	end
	
	raise EOFError, "Could not read frame header!"
end

#write_frame(frame) ⇒ Object



69
70
71
# File 'lib/protocol/websocket/framer.rb', line 69

def write_frame(frame)
	frame.write(@stream)
end