diff --git a/examples/chat.rb b/examples/chat.rb new file mode 100755 index 00000000..e0575f3c --- /dev/null +++ b/examples/chat.rb @@ -0,0 +1,61 @@ +#!/usr/bin/env ruby -I ../lib -I lib +# coding: utf-8 +require 'sinatra' +set :server, 'thin' +connections = [] + +get '/' do + halt erb(:login) unless params[:user] + erb :chat, :locals => { :user => params[:user].gsub(/\W/, '') } +end + +get '/stream', :provides => 'text/event-stream' do + stream :keep_open do |out| + connections << out + out.callback { connections.delete(out) } + end +end + +post '/' do + connections.each { |out| out << "data: #{params[:msg]}\n\n" } + 204 # response without entity body +end + +__END__ + +@@ layout + + + Super Simple Chat with Sinatra + + + + <%= yield %> + + +@@ login +
+ + + +
+ +@@ chat +

+
+
+
+
+ +
\ No newline at end of file diff --git a/examples/simple.rb b/examples/simple.rb new file mode 100755 index 00000000..2697f94b --- /dev/null +++ b/examples/simple.rb @@ -0,0 +1,3 @@ +#!/usr/bin/env ruby -I ../lib -I lib +require 'sinatra' +get('/') { 'this is a simple app' } diff --git a/examples/stream.ru b/examples/stream.ru new file mode 100644 index 00000000..074c66b9 --- /dev/null +++ b/examples/stream.ru @@ -0,0 +1,26 @@ +# this example does *not* work properly with WEBrick +# +# run *one* of these: +# +# rackup -s mongrel stream.ru # gem install mongrel +# thin -R stream.ru start # gem install thin +# unicorn stream.ru # gem install unicorn +# puma stream.ru # gem install puma + +require 'sinatra/base' + +class Stream < Sinatra::Base + get '/' do + content_type :txt + + stream do |out| + out << "It's gonna be legen -\n" + sleep 0.5 + out << " (wait for it) \n" + sleep 1 + out << "- dary!\n" + end + end +end + +run Stream