Sha256: c0b5402f1ae912a0ae6b4704513e15d8090032223eaa912ba9eff76a61c1d49e

Contents?: true

Size: 991 Bytes

Versions: 6

Compression:

Stored size: 991 Bytes

Contents

# Example from https://github.com/socketry/nio4r/blob/master/examples/echo_server.rb
# rewrite it in lightio for demonstrate
# this example demonstrate LightIO Libraries API
# look LightIO::Library namespace to find more

require 'lightio'

class EchoServer
  def initialize(host, port)
    @server = LightIO::TCPServer.new(host, port)
  end

  def run
    while (socket = @server.accept)
      _, port, host = socket.peeraddr
      puts "accept connection from #{host}:#{port}"

      # LightIO::Beam is lightweight executor, provide thread-like interface
      # just start new beam for per socket
      LightIO::Beam.new(socket) do |socket|
        loop do
          echo(socket)
        end
      end
    end
  end

  def echo(socket)
    data = socket.readpartial(4096)
    socket.write(data)
  rescue EOFError
    _, port, host = socket.peeraddr
    puts "*** #{host}:#{port} disconnected"
    socket.close
    raise
  end
end


EchoServer.new('localhost', 3000).run if __FILE__ == $0

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
lightio-0.4.4 examples/echo_server.rb
lightio-0.4.3 examples/echo_server.rb
lightio-0.4.2 examples/echo_server.rb
lightio-0.4.1 examples/echo_server.rb
lightio-0.4.0 examples/echo_server.rb
lightio-0.4.0.pre examples/echo_server.rb