Skip to content

Instantly share code, notes, and snippets.

@akdubya
Created April 24, 2009 01:22
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 akdubya/100876 to your computer and use it in GitHub Desktop.
Save akdubya/100876 to your computer and use it in GitHub Desktop.
require 'eventmachine'
require 'fiber'
Q = Array.new
worker = Fiber.new do
loop do
Fiber.yield
blk = Q.shift
blk.call if blk
end
end
module QueuedEchoServer
def receive_data(data)
Q << lambda { send_data(data.chomp + ': ' + Time.now.to_s + "[#{Q.size}]\n") }
send_data "added input to the queue...\n"
end
end
EM.run do
host = '0.0.0.0'
port = 8080
EM.start_server host, port, QueuedEchoServer
puts "Started QueuedEchoServer on #{host}:#{port}..."
EM.add_periodic_timer(5) do
worker.resume
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment