Ruby/Network/Server by gserver
Материал из Wiki.crossplatform.ru
Содержание |
create multiple servers
require "gserver" class HelloServer < GServer def serve(io) io.puts("Say something to me:") line = io.gets io.puts("You said "#{line.chomp}"") end end server = HelloServer.new(1234, "127.0.0.1", 1) server.start server2 = HelloServer.new(1235, "127.0.0.1", 1) server2.start sleep 10
GServer
require "gserver" class HelloServer < GServer def serve(io) io.puts("Hello!") end end server = HelloServer.new(1234) server.start server.join
manage currently running servers by using several methods GServer makes available to start, stop, and check servers
require "gserver" class HelloServer < GServer def serve(io) io.puts("To stop this server, type "shutdown"") self.stop if io.gets =~ /shutdown/ end end server = HelloServer.new(1234) server.start loop do break if server.stopped? end puts "Server has been terminated"
Set a maximum number of allowed connections by supplying more parameters to new
require "gserver" class HelloServer < GServer def serve(io) io.puts("Say something to me:") line = io.gets io.puts("You said "#{line.chomp}"") end end server = HelloServer.new(1234, "127.0.0.1", 1) server.start server.join