Skip to content

Commit

Permalink
Merge pull request #15070 from ayamomiji/sse-patch
Browse files Browse the repository at this point in the history
Add multiple lines message support for SSE module
  • Loading branch information
tenderlove committed May 14, 2014
2 parents 09cfa3f + 7a84f0b commit 125cc78
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
3 changes: 2 additions & 1 deletion actionpack/lib/action_controller/metal/live.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ def perform_write(json, options)
end end
end end


@stream.write "data: #{json}\n\n" message = json.gsub("\n", "\ndata: ")
@stream.write "data: #{message}\n\n"
end end
end end


Expand Down
16 changes: 16 additions & 0 deletions actionpack/test/controller/live_stream_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ def sse_with_id
ensure ensure
sse.close sse.close
end end

def sse_with_multiple_line_message
sse = SSE.new(response.stream)
sse.write("first line.\nsecond line.")
ensure
sse.close
end
end end


tests SSETestController tests SSETestController
Expand Down Expand Up @@ -87,6 +94,15 @@ def test_sse_with_id
assert_match(/data: {\"name\":\"Ryan\"}/, second_response) assert_match(/data: {\"name\":\"Ryan\"}/, second_response)
assert_match(/id: 2/, second_response) assert_match(/id: 2/, second_response)
end end

def test_sse_with_multiple_line_message
get :sse_with_multiple_line_message

wait_for_response_stream_close
first_response, second_response = response.body.split("\n")
assert_match(/data: first line/, first_response)
assert_match(/data: second line/, second_response)
end
end end


class LiveStreamTest < ActionController::TestCase class LiveStreamTest < ActionController::TestCase
Expand Down

0 comments on commit 125cc78

Please sign in to comment.