Skip to content

Instantly share code, notes, and snippets.

@lifo
Created January 9, 2010 19:18
Show Gist options
  • Select an option

  • Save lifo/273045 to your computer and use it in GitHub Desktop.

Select an option

Save lifo/273045 to your computer and use it in GitHub Desktop.

Revisions

  1. lifo revised this gist Jan 9, 2010. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions websock_action.rb
    Original 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
  2. lifo revised this gist Jan 9, 2010. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions websock_action.rb
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    # Requires Cramp 0.8+
    require 'rubygems'
    require 'cramp/controller'

  3. lifo created this gist Jan 9, 2010.
    39 changes: 39 additions & 0 deletions websock_action.rb
    Original 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
    28 changes: 28 additions & 0 deletions websocket_consumer.html
    Original 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>