Skip to content

Instantly share code, notes, and snippets.

@dcadenas
Created April 5, 2010 04:18
Show Gist options
  • Select an option

  • Save dcadenas/356014 to your computer and use it in GitHub Desktop.

Select an option

Save dcadenas/356014 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'preforker'
require 'eventmachine'
class EchoServer < EM::Connection
def notify_readable
while socket = @io.accept_nonblock
message = socket.gets
socket.write message
socket.close
end
rescue Errno::EAGAIN, Errno::ECONNABORTED
end
def close
puts "closing"
detach
@io.close
end
end
socket = TCPServer.new("0.0.0.0", 8081)
socket.listen(10)
EventMachine.epoll
EventMachine::run do
EM.watch(socket, EchoServer, self){ |c| c.notify_readable = true}
puts "Listening..."
end
@dcadenas
Copy link
Author

dcadenas commented Apr 5, 2010

require 'rubygems'
require 'preforker'
require 'eventmachine'

class EchoServer < EM::Connection
def notify_readable
while socket = @io.accept_nonblock
message = socket.gets
socket.write message
socket.close
end
rescue Errno::EAGAIN, Errno::ECONNABORTED
end

def close
puts "closing"
detach
@io.close
end
end

socket = TCPServer.new("0.0.0.0", 8081)
socket.listen(10)

EventMachine.epoll
EventMachine::run do
EM.watch(socket, EchoServer, self){ |c| c.notify_readable = true}
puts "Listening..."
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment