我正在尝试编写一个EventMachine服务器,通过telnet或ssh运行nCurses应用程序,使用ruby 1.9.3的PTY和io / console模块:
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
}
如果我使用传递给@ reader.read的参数值,我可以在客户端显示部分屏幕,但是我不知道如何确定客户端终端的实际大小,无论是初始还是在客户端调整终端大小后。
如何将输出与客户端终端的大小同步?是否可以使用telnet / netcat或者我必须使用更强大的协议,如ssh?
提前致谢。
答案 0 :(得分:0)
也许这可以帮到你。不过,我正在使用EventMachine和ZeroMQ,但似乎很容易适应。
def run_command command
_start = Time.now
begin
PTY.spawn( command ) do |r, w, pid|
r.sync = true
begin
r.each do |line|
$announce_socket.send_msg "LOG:#{line}"
end
rescue => ex
puts "ERROR: #{ex.message}"
end
end
rescue PTY::ChildExited => e
$announce_socket.send_msg "CMD_PREMATURE_EXIT:#{e.message}"
puts "command exited prematurely, notified UI"
end
$announce_socket.send_msg "CMD_EXIT:#{Time.now - _start}"
puts "done running commmand (took #{Time.now - _start} seconds)"
end
end
反应堆部分:
$context = EM::ZeroMQ::Context.new(1)
def cleanup(exit_code = 0)
EM::stop()
exit exit_code
end
trap('INT') do
cleanup
end
EM.run do
$announce_socket = $context.socket ZMQ::DEALER, EMDEALERHandler.new
$announce_socket.identity = MY_ID
$announce_socket.connect "tcp://#{host_ip}:7777"
# announce presence
$announce_socket.send_msg "UP"
end
在此之后,你只需要一个ROUTER套接字来推送数据。