Skip to content

Instantly share code, notes, and snippets.

@bugant
Created February 22, 2012 16:46
Show Gist options
  • Save bugant/1885977 to your computer and use it in GitHub Desktop.
Save bugant/1885977 to your computer and use it in GitHub Desktop.
em-synchrony stupid udp server
require "em-synchrony"
require "em-synchrony/em-http"
class EchoUdpServer < EventMachine::Connection
def post_init
@f = Fiber.current
end
def receive_data(data)
@f.resume(data)
end
end
EM.synchrony do
EventMachine::open_datagram_socket("127.0.0.1", 7878, EchoUdpServer)
puts "UDP server listening on port 7878..."
while true
data = Fiber.yield
if data == "quit"
puts "quitting"
EventMachine.stop
exit
end
url = "http://#{data}"
puts "getting: #{url}"
resp = EventMachine::HttpRequest.new(url).get
if resp.response_header.status == 200
puts "#{data} is up and running!"
else
puts "#{data} is down!"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment