Permalink
Browse files

Cramp goes SSE

  • Loading branch information...
lifo committed Feb 13, 2011
1 parent 1c41c99 commit 56dea0b78d335d6e37163fb0b3911199e8dc8f9a
Showing with 23 additions and 0 deletions.
  1. +1 −0 lib/cramp.rb
  2. +22 −0 lib/cramp/sse.rb
View
@@ -20,6 +20,7 @@ module Cramp
autoload :Action, "cramp/action"
autoload :Websocket, "cramp/websocket"
+ autoload :SSE, "cramp/sse"
autoload :Body, "cramp/body"
autoload :PeriodicTimer, "cramp/periodic_timer"
autoload :KeepConnectionAlive, "cramp/keep_connection_alive"
View
@@ -0,0 +1,22 @@
+module Cramp
+ class SSE < Abstract
+ include PeriodicTimer
+
+ def render(data)
+ result = "id: #{event_id}\n"
+ data.split(/\n/).each {|d| result << "data: #{d}\n" }
+ result << "\n"
+
+ @body.call(result)
+ end
+
+ def event_id
+ @event_id ||= Time.now.to_i
+ end
+
+ def respond_with
+ [200, {'Content-Type' => 'text/event-stream', 'Cache-Control' => 'no-cache', 'Connection' => 'keep-alive'}]
+ end
+
+ end
+end

0 comments on commit 56dea0b

Please sign in to comment.