Sha256: e040b200ec911d76e87405dd4a975e8684b2c65f6f7c293db56379c7e608510d

Contents?: true

Size: 865 Bytes

Versions: 11

Compression:

Stored size: 865 Bytes

Contents

#!/usr/bin/env ruby
$:<< '../lib' << 'lib'

#
# A simple HTTP streaming API which returns a 200 response for any GET request
# and then emits numbers 1 through 10 in 1 second intervals using Chunked
# transfer encoding, and finally closes the connection.
#
# Chunked transfer streaming works transparently with both browsers and
# streaming consumers.
#

require 'goliath'

class ChunkedStreaming < Goliath::API
  def on_close(env)
    env.logger.info "Connection closed."
  end

  def response(env)
    i = 0
    pt = EM.add_periodic_timer(1) do
      env.chunked_stream_send("#{i}\n")
      i += 1
    end

    EM.add_timer(10) do
      pt.cancel

      env.chunked_stream_send("!! BOOM !!\n")
      env.chunked_stream_close
    end

    headers = { 'Content-Type' => 'text/plain', 'X-Stream' => 'Goliath' }
    chunked_streaming_response(200, headers)
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
goliath-1.0.7 examples/chunked_streaming.rb
goliath-1.0.6 examples/chunked_streaming.rb
goliath-1.0.5 examples/chunked_streaming.rb
goliath-1.0.4 examples/chunked_streaming.rb
goliath-1.0.3 examples/chunked_streaming.rb
goliath-1.0.2 examples/chunked_streaming.rb
goliath-1.0.1 examples/chunked_streaming.rb
goliath-1.0.0 examples/chunked_streaming.rb
goliath-1.0.0.beta.1 examples/chunked_streaming.rb
goliath-0.9.4 examples/chunked_streaming.rb
goliath-0.9.2 examples/chunked_streaming.rb