Skip to content

Instantly share code, notes, and snippets.

@amscotti
Created April 17, 2013 00:22
Show Gist options
  • Select an option

  • Save amscotti/5400781 to your computer and use it in GitHub Desktop.

Select an option

Save amscotti/5400781 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'em-websocket'
require 'yajl'
require 'haml'
require 'sinatra/base'
require 'thin'
EventMachine.run do
class App < Sinatra::Base
get '/' do
haml :index
end
end
@channel = EM::Channel.new
EventMachine::WebSocket.start(:host => '0.0.0.0', :port => 8080) do |ws|
ws.onopen {
sid = @channel.subscribe { |msg| ws.send msg }
@channel.push "#{sid} connected!"
ws.onmessage { |msg|
@channel.push "<#{sid}>: #{msg}"
}
ws.onclose {
@channel.unsubscribe(sid)
}
}
end
App.run!({:port => 3000})
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment