Sha256: b7e7776e29ff965732b17958160baba37566e87d756e503b471dd5a8bdcc9872

Contents?: true

Size: 896 Bytes

Versions: 3

Compression:

Stored size: 896 Bytes

Contents

#!/usr/bin/env ruby
#
# Run this as: bundle exec examples/echo_server.rb

require 'bundler/setup'
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)
    async.run
  end

  def finalize
    @server.close if @server
  end

  def run
    loop { async.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

3 entries across 3 versions & 1 rubygems

Version Path
celluloid-io-0.13.1 examples/echo_server.rb
celluloid-io-0.13.0 examples/echo_server.rb
celluloid-io-0.13.0.pre2 examples/echo_server.rb