Skip to content

Instantly share code, notes, and snippets.

@luckyruby
Created April 18, 2012 23:53
Show Gist options
  • Save luckyruby/2417434 to your computer and use it in GitHub Desktop.
Save luckyruby/2417434 to your computer and use it in GitHub Desktop.
require 'celluloid/io'
class EchoServer
include Celluloid::IO
def initialize(host, port)
puts "*** Starting echo server on #{host}:#{port}"
# Since we included Celluloid::IO, we're actually making a
# Celluloid::IO::TCPServer here
@server = TCPServer.new(host, port)
run!
end
def finalize
@server.close if @server
end
def run
loop { handle_connection! @server.accept }
end
def handle_connection(socket)
_, port, host = socket.peeraddr
puts "*** Received connection from #{host}:#{port}"
loop { socket.write socket.readpartial(4096) }
rescue EOFError
puts "*** #{host}:#{port} disconnected"
end
end
EchoServer.new('localhost', 5000)
*** Starting echo server on localhost:5000
I, [2012-04-18T19:44:09.638580 #11337] INFO -- : Terminating 1 actors...
I, [2012-04-18T19:44:09.639408 #11337] INFO -- : Shutdown completed cleanly
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment