Skip to content

Instantly share code, notes, and snippets.

@jots
Created January 2, 2012 00:12
Show Gist options
  • Save jots/1548743 to your computer and use it in GitHub Desktop.
Save jots/1548743 to your computer and use it in GitHub Desktop.
em.watch
require 'rubygems'
require 'em-websocket'
require 'json'
require 'open4'
EventMachine.run {
EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 4452) do |ws|
ws.onopen {
puts "WebSocket connection open"
ws.send "Hello Client"
}
ws.onclose { puts "Connection closed" }
ws.onmessage { |msg|
puts "Recieved message: #{msg}"
d = JSON.parse(msg)
cmd = d["cmd"]
if cmd
pid, stdin, stdout, stderr = Open4::popen4 "#{cmd}"
EM.watch(stdout) { |o|
# XXX How do I get notified of output?
pp o
da = o.io.read
ws.send "stdout: #{da}"
}
EM.watch(stderr) { |o|
# XXX How do I get notified of output?
pp o
da = o.io.read
ws.send "stderr: #{da}"
}
end
input = d["input"]
if input
stdin.printf("#{input}\n")
end
}
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment