Sha256: 725cefe8d96b11df7324b082e74a4edbdb670cadd82d46bce493deba36b59275

Contents?: true

Size: 857 Bytes

Versions: 5

Compression:

Stored size: 857 Bytes

Contents

#!/usr/bin/env ruby

$:.push File.expand_path('../../lib', __FILE__)
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"
    socket.close
  end
end

supervisor = EchoServer.supervise("127.0.0.1", 1234)
trap("INT") { supervisor.terminate; exit }
sleep

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
celluloid-io-0.12.1 examples/echo_server.rb
celluloid-io-0.12.0 examples/echo_server.rb
celluloid-io-0.11.0 examples/echo_server.rb
celluloid-io-0.10.0 examples/echo_server.rb
celluloid-io-0.9.0 examples/echo_server.rb