lib/websocket/eventmachine/base.rb in websocket-eventmachine-base-1.0.0 vs lib/websocket/eventmachine/base.rb in websocket-eventmachine-base-1.0.1

- old
+ new

@@ -46,10 +46,11 @@ # @option args [String] :type Type of frame to send - available types are "text", "binary", "ping", "pong" and "close" # @option args [Integer] :code Code for close frame # @return [Boolean] true if data was send, otherwise call on_error if needed def send(data, args = {}) type = args[:type] || :text + return if @state == :closed || (@state == :closing && type != :close) unless type == :plain frame = outgoing_frame.new(:version => @handshake.version, :data => data, :type => type.to_s, :code => args[:code]) if !frame.supported? trigger_onerror("Frame type '#{type}' is not supported in protocol version #{@handshake.version}") return false @@ -154,32 +155,38 @@ end def handle_open(data) @frame << data while frame = @frame.next - case frame.type - when :close - @state = :closing - close - trigger_onclose(frame.to_s) - when :ping - pong(frame.to_s) - trigger_onping(frame.to_s) - when :pong - trigger_onpong(frame.to_s) - when :text - trigger_onmessage(frame.to_s, :text) - when :binary - trigger_onmessage(frame.to_s, :binary) + if @state == :open + case frame.type + when :close + @state = :closing + close + trigger_onclose(frame.to_s) + when :ping + pong(frame.to_s) + trigger_onping(frame.to_s) + when :pong + trigger_onpong(frame.to_s) + when :text + trigger_onmessage(frame.to_s, :text) + when :binary + trigger_onmessage(frame.to_s, :binary) + end + else + break end end unbind if @frame.error? end def handle_closing(data) - @state = :closed - close - trigger_onclose + unless @state == :closed + @state = :closed + close + trigger_onclose('') + end end def debug(description, data) return unless @debug puts(description + data.bytes.to_a.collect{|b| '\x' + b.to_s(16).rjust(2, '0')}.join) unless @state == :connecting