Skip to content

Instantly share code, notes, and snippets.

@kenn
Created August 13, 2009 23:10
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 kenn/167509 to your computer and use it in GitHub Desktop.
Save kenn/167509 to your computer and use it in GitHub Desktop.
require 'eventmachine'
require 'pp'
$stdout.sync = true
class KeyboardHandler < EM::Connection
include EM::Protocols::LineText2
def post_init
print "> "
end
def receive_line(line)
line.chomp!
line.gsub!(/^\s+/, '')
case(line)
when /^udp (.*)$/
data = $1.chomp
EventMachine::send_datagram UdpClient.signature, data, data.length, 'localhost', 8082
when /^exit$/
EM.stop
return
end
print "> "
end
end
class UdpClient < EM::Connection
def self.new(sig, *args)
@@signature = sig
super
end
def self.signature
@@signature
end
def receive_data(data)
puts "Received: #{data.to_s}"
print "> "
end
end
EM::run {
EM.open_keyboard(KeyboardHandler)
EM.open_datagram_socket("0.0.0.0", 0, UdpClient)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment