Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Makes it easy to upgrade any request to a websocket connection in Sinatra

branch: master

This branch is 0 commits ahead and 0 commits behind master

Octocat-spinner-32 examples run from cli April 03, 2013
Octocat-spinner-32 lib version bump April 03, 2013
Octocat-spinner-32 spec adds basic unit tests for error module April 03, 2013
Octocat-spinner-32 .gitignore bump version October 09, 2012
Octocat-spinner-32 CHANGELOG.md version bump April 03, 2013
Octocat-spinner-32 LICENSE.txt renamed and restructured April 25, 2012
Octocat-spinner-32 README.md typo fix November 02, 2013
Octocat-spinner-32 Rakefile Forgot to change this. October 09, 2011
Octocat-spinner-32 VERSION Version 0.2.0, hooray! October 09, 2011
Octocat-spinner-32 sinatra-websocket.gemspec refs #3 use em-websocket between 0.3.6 and 0.3.X February 20, 2013
README.md

SinatraWebsocket

Makes it easy to upgrade any request to a websocket connection.

SinatraWebsocket is a fork of Skinny merged with Rack WebSocket. It provides helpers methods to detect if a request is a WebSocket request and defer to an EM::WebSocket::Connection.

Put this in your pipe ...

require 'sinatra'
require 'sinatra-websocket'

set :server, 'thin'
set :sockets, []

get '/' do
  if !request.websocket?
    erb :index
  else
    request.websocket do |ws|
      ws.onopen do
        ws.send("Hello World!")
        settings.sockets << ws
      end
      ws.onmessage do |msg|
        EM.next_tick { settings.sockets.each{|s| s.send(msg) } }
      end
      ws.onclose do
        warn("websocket closed")
        settings.sockets.delete(ws)
      end
    end
  end
end

__END__
@@ index
<html>
  <body>
     <h1>Simple Echo & Chat Server</h1>
     <form id="form">
       <input type="text" id="input" value="send a message"></input>
     </form>
     <div id="msgs"></div>
  </body>

  <script type="text/javascript">
    window.onload = function(){
      (function(){
        var show = function(el){
          return function(msg){ el.innerHTML = msg + '<br />' + el.innerHTML; }
        }(document.getElementById('msgs'));

        var ws       = new WebSocket('ws://' + window.location.host + window.location.pathname);
        ws.onopen    = function()  { show('websocket opened'); };
        ws.onclose   = function()  { show('websocket closed'); }
        ws.onmessage = function(m) { show('websocket message: ' +  m.data); };

        var sender = function(f){
          var input     = document.getElementById('input');
          input.onclick = function(){ input.value = "" };
          f.onsubmit    = function(){
            ws.send(input.value);
            input.value = "send a message";
            return false;
          }
        }(document.getElementById('form'));
      })();
    }
  </script>
</html>

And Smoke It

ruby echo.rb

Copyright

Copyright (c) 2012 Caleb Crane.

Portions of this software are Copyright (c) Bernard Potocki bernard.potocki@imanel.org and Samuel Cochran.

See License.txt for more details.

Something went wrong with that request. Please try again.