lib/faye/websocket/api.rb in faye-websocket-0.9.1 vs lib/faye/websocket/api.rb in faye-websocket-0.9.2
- old
+ new
@@ -34,26 +34,52 @@
@ready_state = CONNECTING
@buffered_amount = 0
@driver.on(:open) { |e| open }
@driver.on(:message) { |e| receive_message(e.data) }
- @driver.on(:close) { |e| finalize(e.reason, e.code) }
+ @driver.on(:close) { |e| begin_close(e.reason, e.code) }
@driver.on(:error) do |error|
- event = Event.create('error', :message => error.message)
- event.init_event('error', false, false)
- dispatch_event(event)
+ emit_error(error.message)
end
if @ping
@ping_timer = EventMachine.add_periodic_timer(@ping) do
@ping_id += 1
ping(@ping_id.to_s)
end
end
end
+ def write(data)
+ @stream.write(data)
+ end
+
+ def send(message)
+ return false if @ready_state > OPEN
+ case message
+ when Numeric then @driver.text(message.to_s)
+ when String then @driver.text(message)
+ when Array then @driver.binary(message)
+ else false
+ end
+ end
+
+ def ping(message = '', &callback)
+ return false if @ready_state > OPEN
+ @driver.ping(message, &callback)
+ end
+
+ def close
+ @ready_state = CLOSING unless @ready_state == CLOSED
+ @driver.close
+ end
+
+ def protocol
+ @driver.protocol || ''
+ end
+
private
def open
return unless @ready_state == CONNECTING
@ready_state = OPEN
@@ -67,52 +93,46 @@
event = Event.create('message', :data => data)
event.init_event('message', false, false)
dispatch_event(event)
end
- def finalize(reason = nil, code = nil)
- return if @ready_state == CLOSED
- @ready_state = CLOSED
- EventMachine.cancel_timer(@ping_timer) if @ping_timer
- @stream.close_connection_after_writing if @stream
- event = Event.create('close', :code => code || 1000, :reason => reason || '')
- event.init_event('close', false, false)
+ def emit_error(message)
+ return if @ready_state >= CLOSING
+
+ event = Event.create('error', :message => message)
+ event.init_event('error', false, false)
dispatch_event(event)
end
- def parse(data)
- worker = @proxy || @driver
- worker.parse(data)
+ def begin_close(reason, code)
+ return if @ready_state == CLOSED
+ @ready_state = CLOSING
+
+ if @stream
+ @stream.close_connection_after_writing
+ else
+ finalize_close
+ end
+ @close_params = [reason, code]
end
- public
+ def finalize_close
+ return if @ready_state == CLOSED
+ @ready_state = CLOSED
- def write(data)
- @stream.write(data)
- end
+ EventMachine.cancel_timer(@ping_timer) if @ping_timer
- def send(message)
- return false if @ready_state > OPEN
- case message
- when Numeric then @driver.text(message.to_s)
- when String then @driver.text(message)
- when Array then @driver.binary(message)
- else false
- end
- end
+ reason = @close_params ? @close_params[0] : ''
+ code = @close_params ? @close_params[1] : 1006
- def ping(message = '', &callback)
- return false if @ready_state > OPEN
- @driver.ping(message, &callback)
+ event = Event.create('close', :code => code, :reason => reason)
+ event.init_event('close', false, false)
+ dispatch_event(event)
end
- def close
- @ready_state = CLOSING unless @ready_state == CLOSED
- @driver.close
- end
-
- def protocol
- @driver.protocol || ''
+ def parse(data)
+ worker = @proxy || @driver
+ worker.parse(data)
end
end
end
end