Created
April 24, 2009 01:22
-
-
Save akdubya/100876 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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