Created
April 18, 2012 23:53
-
-
Save luckyruby/2417434 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 '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) |
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
*** 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