Skip to content

Commit

Permalink
Slim down the web socket respond blocks
Browse files Browse the repository at this point in the history
Move heartbeat into on_open/close and add a similarly named on_message
to handle that callback.
  • Loading branch information
dhh committed Jun 22, 2015
1 parent 24609f1 commit aaad3ea
Showing 1 changed file with 12 additions and 17 deletions.
29 changes: 12 additions & 17 deletions lib/action_cable/connection/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,12 @@ def process
if websocket?
@websocket = Faye::WebSocket.new(@env)

@websocket.on(:open) do |event|
heartbeat.start
send_async :on_open
end

@websocket.on(:message) do |event|
message_buffer.append event.data
end

@websocket.on(:close) do |event|
logger.info finished_request_message

heartbeat.stop
send_async :on_close
end
@websocket.on(:open) { |event| send_async :on_open }
@websocket.on(:message) { |event| on_message event.data }
@websocket.on(:close) { |event| send_async :on_close }

@websocket.rack_response
else
logger.info finished_request_message

respond_to_invalid_request
end
end
Expand Down Expand Up @@ -108,16 +94,24 @@ def on_open

connect if respond_to?(:connect)
subscribe_to_internal_channel
heartbeat.start

message_buffer.process!
end

def on_message(message)
message_buffer.append event.data
end

def on_close
logger.info finished_request_message

server.remove_connection(self)

subscriptions.cleanup
unsubscribe_from_internal_channel
heartbeat.stop

disconnect if respond_to?(:disconnect)
end

Expand All @@ -135,6 +129,7 @@ def decode_json(json)
end

def respond_to_invalid_request
logger.info finished_request_message
[ 404, { 'Content-Type' => 'text/plain' }, [ 'Page not found' ] ]
end

Expand Down

0 comments on commit aaad3ea

Please sign in to comment.