I'm trying to write an EventMachine server to run an nCurses application via telnet or ssh, using ruby 1.9.3's PTY and io/console modules:
require 'rubygems'
require 'socket'
require 'pty'
require 'io/console'
require 'eventmachine'
module CursesServer
def post_init
puts "-- someone connected to the server!"
@reader, @writer, @pid = PTY.spawn('ruby ./ncurses_app_to_run.rb')
@reader.sync = true #No buffering stdout
end
def receive_data(data)
screen = @reader.read(<number_of_bytes>)
puts "received: " + data # Log input on server side
send_data screen
close_connection if data =~ /quit/i
end
def unbind
puts "-- someone disconnected from the server!"
end
end
# Note that this will block current thread.
EventMachine.run {
EventMachine.start_server "127.0.0.1", 8081, CursesServer
}
If I play with the value of the argument passed to @reader.read, I can get portions of the screen to display on the client side, however I don't know how to determine the actual size of the client's terminal, either initally or after the client resizes the terminal.
How can I synchronize the output with the size of the client's terminal? Is it possible with telnet/netcat or do I have to use a more robust protocol like ssh?
Thanks in advance.