Skip to content

Instantly share code, notes, and snippets.

@abruzzi
Created June 2, 2017 05:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abruzzi/1690bb4b437617e09210de0c5ec54122 to your computer and use it in GitHub Desktop.
Save abruzzi/1690bb4b437617e09210de0c5ec54122 to your computer and use it in GitHub Desktop.
source 'https://gems.ruby-china.org'
gem 'sinatra'
gem 'sinatra-websocket'
require 'sinatra'
require 'sinatra-websocket'
set :server, 'thin'
set :sockets, []
get '/ws' do
request.websocket do |ws|
ws.onopen do
ws.send("connected")
settings.sockets << ws
end
ws.onmessage do |msg|
p 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment