Exposing console apps to the web with Ruby

Posted by Aaron on Stack Overflow See other posts from Stack Overflow or by Aaron
Published on 2010-02-28T04:24:01Z Indexed on 2010/03/12 8:07 UTC
Read the original article Hit count: 193

Filed under:
|
|

I'm looking to expose an interactive command line program via JSON or another RPC style service using Ruby. I've found a couple tricks to do this, but im missing something when redirecting the output and input.

One method at least on linux is to redirect the stdin and stdout to a file then read and write to that file asynchronously with file reads and writes. Another method ive been trying after googling around was to use open4. Here is the code I wrote so far, but its getting stuck after reading a few lines from standard output.

require "open4"
include Open4

status = popen4("./srcds_run -console -game tf +map ctf_2fort -maxplayers 6") do |pid, stdin, stdout, stderr|
  puts "PID #{pid}"
  lines=""
  while (line=stdout.gets)
    lines+=line
    puts line
  end
  while (line=stderr.gets)
    lines+=line
    puts line
  end
end

Any help on this or some insight would be appreciated!

© Stack Overflow or respective owner

Related posts about ruby

Related posts about stdout