Skip to content

Commit

Permalink
Extract the method to handle headers.
Browse files Browse the repository at this point in the history
Signed-off-by: François de Metz <francois@stormz.me>
  • Loading branch information
francois2metz committed May 16, 2012
1 parent 7888e16 commit 38d4329
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions lib/em-eventsource.rb
Expand Up @@ -111,22 +111,9 @@ def close

def listen
@conn, @req = prepare_request
@req.headers(&method(:handle_headers))
@req.errback(&method(:handle_reconnect))
@req.callback(&method(:handle_reconnect))
@req.headers do |headers|
if headers.status != 200
close
@errors.each { |error| error.call("Unexpected response status #{headers.status}") }
next
end
if /^text\/event-stream/.match headers['CONTENT_TYPE']
@ready_state = OPEN
@opens.each { |open| open.call }
else
close
@errors.each { |error| error.call("The content-type '#{headers['CONTENT_TYPE']}' is not text/event-stream") }
end
end
buffer = ""
@req.stream do |chunk|
buffer += chunk
Expand All @@ -147,6 +134,21 @@ def handle_reconnect(*args)
end
end

def handle_headers(headers)
if headers.status != 200
close
@errors.each { |error| error.call("Unexpected response status #{headers.status}") }
return
end
if /^text\/event-stream/.match headers['CONTENT_TYPE']
@ready_state = OPEN
@opens.each { |open| open.call }
else
close
@errors.each { |error| error.call("The content-type '#{headers['CONTENT_TYPE']}' is not text/event-stream") }
end
end

def handle_stream(stream)
data = ""
name = nil
Expand Down

0 comments on commit 38d4329

Please sign in to comment.