lib/faye/websocket/api.rb in faye-websocket-0.10.5 vs lib/faye/websocket/api.rb in faye-websocket-0.10.6
- old
+ new
@@ -8,10 +8,12 @@
CONNECTING = 0
OPEN = 1
CLOSING = 2
CLOSED = 3
+ CLOSE_TIMEOUT = 30
+
include EventTarget
extend Forwardable
def_delegators :@driver, :version
@@ -34,11 +36,11 @@
@ping = options[:ping]
@ping_id = 0
@buffered_amount = 0
- @close_params = @ping_timer = @proxy = @stream = nil
+ @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) }
@@ -84,10 +86,12 @@
"#{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) }
end
def protocol
@driver.protocol || ''
end
@@ -131,9 +135,10 @@
def finalize_close
return if @ready_state == CLOSED
@ready_state = CLOSED
+ EventMachine.cancel_timer(@close_timer) if @close_timer
EventMachine.cancel_timer(@ping_timer) if @ping_timer
reason = @close_params ? @close_params[0] : ''
code = @close_params ? @close_params[1] : 1006