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
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.