Created
August 13, 2009 23:10
-
-
Save kenn/167509 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 '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