Skip to content
Browse files

Add multiple lines message support for SSE module

  • Loading branch information...
1 parent 096be96 commit 7a84f0b33d6827195dd6134a12007992f7795905 @ayamomiji ayamomiji committed May 12, 2014
Showing with 18 additions and 1 deletion.
  1. +2 −1 actionpack/lib/action_controller/metal/live.rb
  2. +16 −0 actionpack/test/controller/live_stream_test.rb
View
3 actionpack/lib/action_controller/metal/live.rb
@@ -102,7 +102,8 @@ def perform_write(json, options)
end
end
- @stream.write "data: #{json}\n\n"
+ message = json.gsub("\n", "\ndata: ")
+ @stream.write "data: #{message}\n\n"
end
end
View
16 actionpack/test/controller/live_stream_test.rb
@@ -39,6 +39,13 @@ def sse_with_id
ensure
sse.close
end
+
+ def sse_with_multiple_line_message
+ sse = SSE.new(response.stream)
+ sse.write("first line.\nsecond line.")
+ ensure
+ sse.close
+ end
end
tests SSETestController
@@ -87,6 +94,15 @@ def test_sse_with_id
assert_match(/data: {\"name\":\"Ryan\"}/, second_response)
assert_match(/id: 2/, second_response)
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
class LiveStreamTest < ActionController::TestCase

0 comments on commit 7a84f0b

Please sign in to comment.
Something went wrong with that request. Please try again.