Created
February 22, 2012 16:46
-
-
Save bugant/1885977 to your computer and use it in GitHub Desktop.
em-synchrony stupid udp server
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 "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