diff options
author | ayaya <ayaya@ayaya.tw> | 2014-05-12 23:44:26 +0800 |
---|---|---|
committer | ayaya <ayaya@ayaya.tw> | 2014-05-12 23:54:03 +0800 |
commit | 7a84f0b33d6827195dd6134a12007992f7795905 (patch) | |
tree | 2137a4a8f60e6de39e4e41b8edfa6fe8fd815304 /actionpack | |
parent | 096be96db894854a199ce8cf9a8edeca0f203966 (diff) | |
download | rails-7a84f0b33d6827195dd6134a12007992f7795905.tar.gz rails-7a84f0b33d6827195dd6134a12007992f7795905.tar.bz2 rails-7a84f0b33d6827195dd6134a12007992f7795905.zip |
Add multiple lines message support for SSE module
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/lib/action_controller/metal/live.rb | 3 | ||||
-rw-r--r-- | actionpack/test/controller/live_stream_test.rb | 16 |
2 files changed, 18 insertions, 1 deletions
diff --git a/actionpack/lib/action_controller/metal/live.rb b/actionpack/lib/action_controller/metal/live.rb index acf40b2e16..4c0554d27b 100644 --- a/actionpack/lib/action_controller/metal/live.rb +++ b/actionpack/lib/action_controller/metal/live.rb @@ -102,7 +102,8 @@ module ActionController end end - @stream.write "data: #{json}\n\n" + message = json.gsub("\n", "\ndata: ") + @stream.write "data: #{message}\n\n" end end diff --git a/actionpack/test/controller/live_stream_test.rb b/actionpack/test/controller/live_stream_test.rb index 947f64176b..1dca36374a 100644 --- a/actionpack/test/controller/live_stream_test.rb +++ b/actionpack/test/controller/live_stream_test.rb @@ -39,6 +39,13 @@ module ActionController 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 @@ module ActionController 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 |