Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specifying an SSE event name as an option to render #28

Merged
merged 1 commit into from
Sep 23, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/cramp/action.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def render_long_polling(data, *)


def render_sse(data, options = {}) def render_sse(data, options = {})
result = "id: #{sse_event_id}\n" result = "id: #{sse_event_id}\n"
result << "event: #{options[:event]}\n" if options[:event]
result << "retry: #{options[:retry]}\n" if options[:retry] result << "retry: #{options[:retry]}\n" if options[:retry]


data.split(/\n/).each {|d| result << "data: #{d}\n" } data.split(/\n/).each {|d| result << "data: #{d}\n" }
Expand Down
7 changes: 6 additions & 1 deletion test/controller/sse_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class LiveController < Cramp::Action
def go_sse def go_sse
render "Hello World" render "Hello World"
render "Nothing", :retry => 10 render "Nothing", :retry => 10
render "Hello non-message event type", :event => :different
finish finish
end end
end end
Expand All @@ -28,7 +29,7 @@ def test_headers
end end


def test_body def test_body
get_body_chunks '/', :count => 2 do |chunks| get_body_chunks '/', :count => 3 do |chunks|
# chunk1 = id: 1297999043\ndata: Hello World # chunk1 = id: 1297999043\ndata: Hello World
first_chunk = chunks[0].split("\n") first_chunk = chunks[0].split("\n")
assert first_chunk[0] =~ /\Aid: \d+\Z/, first_chunk.inspect assert first_chunk[0] =~ /\Aid: \d+\Z/, first_chunk.inspect
Expand All @@ -37,6 +38,10 @@ def test_body
second_chunk = chunks[1].split("\n") second_chunk = chunks[1].split("\n")
assert_equal "retry: 10", second_chunk[1] assert_equal "retry: 10", second_chunk[1]
assert_equal "data: Nothing", second_chunk[2] assert_equal "data: Nothing", second_chunk[2]

third_chunk = chunks[2].split("\n")
assert_equal "event: different", third_chunk[1]
assert_equal "data: Hello non-message event type", third_chunk[2]
end end
end end


Expand Down