lib/em-websocket/framing03.rb in em-websocket-0.2.1 vs lib/em-websocket/framing03.rb in em-websocket-0.3.0

- old
+ new

@@ -1,11 +1,11 @@ # encoding: BINARY module EventMachine module WebSocket module Framing03 - + def initialize_framing @data = '' @application_data_buffer = '' # Used for MORE frames end @@ -13,11 +13,11 @@ error = false while !error && @data.size > 1 pointer = 0 - more = (@data.getbyte(pointer) & 0b10000000) == 0b10000000 + more = ((@data.getbyte(pointer) & 0b10000000) == 0b10000000) ^ fin # Ignoring rsv1-3 for now opcode = @data.getbyte(0) & 0b00001111 pointer += 1 # Ignoring rsv4 @@ -26,11 +26,11 @@ payload_length = case length when 127 # Length defined by 8 bytes # Check buffer size if @data.getbyte(pointer+8-1) == nil - debug [:buffer_incomplete, @data.inspect] + debug [:buffer_incomplete, @data] error = true next end # Only using the last 4 bytes for now, till I work out how to @@ -39,11 +39,11 @@ pointer += 8 l when 126 # Length defined by 2 bytes # Check buffer size if @data.getbyte(pointer+2-1) == nil - debug [:buffer_incomplete, @data.inspect] + debug [:buffer_incomplete, @data] error = true next end l = @data[pointer..(pointer+1)].unpack('n').first @@ -53,11 +53,11 @@ length end # Check buffer size if @data.getbyte(pointer+payload_length-1) == nil - debug [:buffer_incomplete, @data.inspect] + debug [:buffer_incomplete, @data] error = true next end # Throw away data up to pointer @@ -89,10 +89,12 @@ end end # end while end def send_frame(frame_type, application_data) + debug [:sending_frame, frame_type, application_data] + if @state == :closing && data_frame?(frame_type) raise WebSocketError, "Cannot send data frame since connection is closing" end frame = '' @@ -122,34 +124,11 @@ send_frame(:text, data) end private - def message(message_type, extension_data, application_data) - case message_type - when :close - if @state == :closing - # TODO: Check that message body matches sent data - # We can close connection immediately since there is no more data - # is allowed to be sent or received on this connection - @connection.close_connection - @state = :closed - else - # Acknowlege close - # The connection is considered closed - send_frame(:close, application_data) - @state = :closed - @connection.close_connection_after_writing - end - when :ping - # Pong back the same data - send_frame(:pong, application_data) - when :pong - # TODO: Do something. Complete a deferrable established by a ping? - when :text, :binary - @connection.trigger_on_message(application_data) - end - end + # This allows flipping the more bit to fin for draft 04 + def fin; false; end FRAME_TYPES = { :continuation => 0, :close => 1, :ping => 2,