Created
January 9, 2010 19:18
-
-
Save lifo/273045 to your computer and use it in GitHub Desktop.
Revisions
-
lifo revised this gist
Jan 9, 2010 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,6 @@ # Requires Cramp 0.8+ require 'rubygems' require 'usher' require 'cramp/controller' Cramp::Controller::Websocket.backend = :thin -
lifo revised this gist
Jan 9, 2010 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,4 @@ # Requires Cramp 0.8+ require 'rubygems' require 'cramp/controller' -
lifo created this gist
Jan 9, 2010 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,39 @@ require 'rubygems' require 'cramp/controller' Cramp::Controller::Websocket.backend = :thin class WebsockAction < Cramp::Controller::Websocket periodic_timer :send_hello_world, :every => 2 on_data :received_data on_finish :socket_closed def start @id = params[:id] || 'unknown id' end def received_data(data) if data =~ /fuck/ render "#{@id}: You cant say fuck in here" finish else render "#{@id}: Got your #{data}" end end def send_hello_world render "#{@id}: Hello from the Server!" end def socket_closed puts "Client gone away :(" end end Thin::Logging.debug = true routes = Usher::Interface.for(:rack) do add('/(:id)').to(WebsockAction) end Rack::Handler::Thin.run routes, :Port => 3000 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,28 @@ <html> <!-- Yanked from http://www.igvita.com/2009/12/22/ruby-websockets-tcp-for-the-browser --> <head> <script src='http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js'></script> <script> $(document).ready(function(){ function debug(str){ $("#debug").append("<p>"+str+"</p>"); }; ws = new WebSocket("ws://0.0.0.0:3000/1"); ws.onmessage = function(evt) { $("#msg").append("<p>"+evt.data+"</p>"); }; ws.onclose = function() { debug("socket closed"); }; ws.onopen = function() { debug("connected..."); ws.send("hello server"); setTimeout('ws.send("5 secs")', 5000); setTimeout('ws.send("10 secs")', 10000); setTimeout('ws.send("15 secs")', 15000); }; }); </script> </head> <body> <div id="debug"></div> <div id="msg"></div> </body> </html>