Skip to content
Browse files

Some examples.

  • Loading branch information...
1 parent 6764070 commit f31fbb57852443893a6e52bedbf527565d46f591 @sj26 committed Nov 1, 2010
Showing with 127 additions and 0 deletions.
  1. +15 −0 examples/async.ru
  2. +69 −0 examples/chat.ru
  3. +17 −0 examples/delayed.ru
  4. +26 −0 examples/echo.ru
View
15 examples/async.ru
@@ -0,0 +1,15 @@
+require 'rubygems'
+require 'sinatra/base'
+require 'sinatra-diet'
+
+class Application < Sinatra::Base
+ register Sinatra::Async
+
+ get '/' do
+ async do
+ 'Hello, world!'
+ end
+ end
+end
+
+run Application
View
69 examples/chat.ru
@@ -0,0 +1,69 @@
+require 'rubygems'
+require 'sinatra/base'
+require 'sinatra-diet'
+
+class Application < Sinatra::Base
+ register Sinatra::WebSocket
+
+ def self.listeners
+ @@listeners ||= []
+ end
+
+ def self.said
+ @@said ||= []
+ end
+
+ def self.say what
+ said << what
+ EM.next_tick do
+ listeners.each do |listener|
+ listener.send_message what
+ end
+ end
+ end
+
+ websocket '/',
+ :on_handshake => proc { |client| listeners << client },
+ :on_message => proc { |client, message| say message },
+ :on_close => proc { |client| listeners.delete client }
+
+ get '/' do
+ <<-HTML
+ <!DOCTYPE html>
+ <html>
+ <head>
+ <title>Simple Chat</title>
+ <script src="http://code.jquery.com/jquery.js"></script>
+ <script type="text/javascript">
+ jQuery(function ($) {
+ var websocket = new WebSocket("#{request.url.sub(/^http/, 'ws')}"),
+ $form = $('form'), $message = $('input[name="message"]', $form);
+ websocket.onmessage = function(message) {
+ $('#said').prepend(message.data + "\\n");
+ };
+ $form.submit(function () {
+ websocket.send($message.val());
+ $message.val("");
+ return false;
+ });
+ $message.focus();
+ });
+ </script>
+ </head>
+ <body>
+ <form action="/say" method="post">
+ <input type="text" name="message" style="width: 100%;" />
+ <pre id="said" style="height: 20em; overflow: auto; border: 1px inset;">#{self.class.said.reverse.collect { |message| message + "\n" }.join ''}</pre>
+ </form>
+ </body>
+ </html>
+ HTML
+ end
+
+ post '/say' do
+ self.class.say params[:message]
+ redirect '/'
+ end
+end
+
+run Application
View
17 examples/delayed.ru
@@ -0,0 +1,17 @@
+require 'rubygems'
+require 'sinatra/base'
+require 'sinatra-diet'
+
+class Application < Sinatra::Base
+ register Sinatra::Async
+
+ get '/' do
+ async do
+ EventMachine::Timer.new(2) do
+ async_respond 'Hello, world!'
+ end
+ end
+ end
+end
+
+run Application
View
26 examples/echo.ru
@@ -0,0 +1,26 @@
+require 'rubygems'
+require 'sinatra/base'
+require 'sinatra-diet'
+
+class Application < Sinatra::Base
+ register Sinatra::WebSocket
+
+ get '/' do
+ '<script>' +
+ 'var websocket = new WebSocket("ws://localhost:3000/echo");' +
+ 'websocket.onmessage=function(message){' +
+ 'document.getElementById(\'transcript\').innerHTML+=message.data+"\n";' +
+ '}' +
+ '</script>' +
+ '<form onsubmit="websocket.send(this.message.value);this.message.value=\'\';return false">' +
+ '<input name="message" type="text" />' +
+ '</form>' +
+ '<pre id="transcript"></pre>'
+ end
+
+ websocket '/echo' do |connection, message|
+ connection.send_message "You said: #{message}"
+ end
+end
+
+run Application

0 comments on commit f31fbb5

Please sign in to comment.
Something went wrong with that request. Please try again.