Sha256: d628b452efa4909abdac426b4726e2cd3d9cbcfd07fbaba44ceade8557cad8e9
Contents?: true
Size: 1.02 KB
Versions: 26
Compression:
Stored size: 1.02 KB
Contents
#!/usr/bin/env ruby # frozen_string_literal: true $LOAD_PATH.push File.expand_path("../lib", __dir__) require "nio" require "socket" # Echo server example written with nio4r class EchoServer def initialize(host, port) @selector = NIO::Selector.new puts "Listening on #{host}:#{port}" @server = TCPServer.new(host, port) monitor = @selector.register(@server, :r) monitor.value = proc { accept } end def run loop do @selector.select { |monitor| monitor.value.call } end end def accept socket = @server.accept _, port, host = socket.peeraddr puts "*** #{host}:#{port} connected" monitor = @selector.register(socket, :r) monitor.value = proc { read(socket) } end def read(socket) data = socket.read_nonblock(4096) socket.write_nonblock(data) rescue EOFError _, port, host = socket.peeraddr puts "*** #{host}:#{port} disconnected" @selector.deregister(socket) socket.close end end EchoServer.new("localhost", 1234).run if $PROGRAM_NAME == __FILE__
Version data entries
26 entries across 23 versions & 5 rubygems