Skip to content

Instantly share code, notes, and snippets.

@kgrz
Last active December 24, 2015 16:09
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 kgrz/6826255 to your computer and use it in GitHub Desktop.
Save kgrz/6826255 to your computer and use it in GitHub Desktop.
require 'em-websocket'
EM.run do
EM::WebSocket.start(:host => '0.0.0.0', :port => 8080) do |ws|
q = EM::Queue.new
l = Mutex.new
ws.onmessage do |msg|
op = proc do
l.synchronize do
q.push Thread.current
q.size.times do
q.pop { |t| t.kill }
end
end
puts "Received #{msg}. will now do a looooong process"
sleep 10 # Thread safe IO here that is safely killed
true
end
callback = proc do |result|
puts "Done! with #{msg}"
end
EM.defer(op, callback)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment