mirror of
				https://github.com/sinatra/sinatra
				synced 2023-03-27 23:18:01 -04:00 
			
		
		
		
	add examples directory
This commit is contained in:
		
							parent
							
								
									42c7bcac62
								
							
						
					
					
						commit
						88406916d8
					
				
					 3 changed files with 90 additions and 0 deletions
				
			
		
							
								
								
									
										61
									
								
								examples/chat.rb
									
										
									
									
									
										Executable file
									
								
							
							
						
						
									
										61
									
								
								examples/chat.rb
									
										
									
									
									
										Executable file
									
								
							| 
						 | 
				
			
			@ -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
 | 
			
		||||
<html>
 | 
			
		||||
  <head> 
 | 
			
		||||
    <title>Super Simple Chat with Sinatra</title> 
 | 
			
		||||
    <meta charset="utf-8" />
 | 
			
		||||
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> 
 | 
			
		||||
  </head> 
 | 
			
		||||
  <body><%= yield %></body>
 | 
			
		||||
</html>
 | 
			
		||||
 | 
			
		||||
@@ login
 | 
			
		||||
<form action='/'>
 | 
			
		||||
  <label for='user'>User Name:</label>
 | 
			
		||||
  <input name='user' value='' />
 | 
			
		||||
  <input type='submit' value="GO!" />
 | 
			
		||||
</form>
 | 
			
		||||
 | 
			
		||||
@@ chat
 | 
			
		||||
<pre id='chat'></pre>
 | 
			
		||||
 | 
			
		||||
<script>
 | 
			
		||||
  // reading
 | 
			
		||||
  var es = new EventSource('/stream');
 | 
			
		||||
  es.onmessage = function(e) { $('#chat').append(e.data + "\n") };
 | 
			
		||||
 | 
			
		||||
  // writing
 | 
			
		||||
  $("form").live("submit", function(e) {
 | 
			
		||||
    $.post('/', {msg: "<%= user %>: " + $('#msg').val()});
 | 
			
		||||
    $('#msg').val(''); $('#msg').focus();
 | 
			
		||||
    e.preventDefault();
 | 
			
		||||
  });
 | 
			
		||||
</script>
 | 
			
		||||
 | 
			
		||||
<form>
 | 
			
		||||
  <input id='msg' placeholder='type message here...' />
 | 
			
		||||
</form>
 | 
			
		||||
							
								
								
									
										3
									
								
								examples/simple.rb
									
										
									
									
									
										Executable file
									
								
							
							
						
						
									
										3
									
								
								examples/simple.rb
									
										
									
									
									
										Executable file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,3 @@
 | 
			
		|||
#!/usr/bin/env ruby -I ../lib -I lib
 | 
			
		||||
require 'sinatra'
 | 
			
		||||
get('/') { 'this is a simple app' }
 | 
			
		||||
							
								
								
									
										26
									
								
								examples/stream.ru
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								examples/stream.ru
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -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
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue