lib/faye/websocket/api.rb in faye-websocket-0.10.7 vs lib/faye/websocket/api.rb in faye-websocket-0.10.8

- old
+ new

@@ -41,11 +41,11 @@ @close_params = @close_timer = @ping_timer = @proxy = @stream = nil @onopen = @onmessage = @onclose = @onerror = nil @driver.on(:open) { |e| open } @driver.on(:message) { |e| receive_message(e.data) } - @driver.on(:close) { |e| begin_close(e.reason, e.code) } + @driver.on(:close) { |e| begin_close(e.reason, e.code, :wait_for_write => true) } @driver.on(:error) do |error| emit_error(error.message) end @@ -85,13 +85,13 @@ "The code must be either 1000, or between 3000 and 4999. " + "#{code} is neither." end @ready_state = CLOSING unless @ready_state == CLOSED - @driver.close(reason, code) - @close_timer = EventMachine.add_timer(CLOSE_TIMEOUT) { begin_close('', 1006) } + + @driver.close(reason, code) end def protocol @driver.protocol || '' end @@ -119,16 +119,20 @@ event = Event.create('error', :message => message) event.init_event('error', false, false) dispatch_event(event) end - def begin_close(reason, code) + def begin_close(reason, code, options = {}) return if @ready_state == CLOSED @ready_state = CLOSING @close_params = [reason, code] if @stream - @stream.close_connection_after_writing + if options[:wait_for_write] + @stream.close_connection_after_writing + else + @stream.close_connection + end else finalize_close end end