question on EM.watch

19 views
Skip to first unread message

Joe

unread,
Jan 1, 2012, 9:08:10 PM1/1/12
to EventMachine
I'm trying to allow a websocket user to open up a process on the
server and interact with it, seeing the stdout and stderr. I'm using
popen4 so that I can get stdout and stderr. I want to use event
machine watch the file descriptors and send the output to the
websocket when new data arrives.

I know the preferred way to use EM.watch is to give it a module as the
second parameter, but I don't know how the module can access the
websocket (ws).

The rough code below shows what I'm trying to do. o.io.read doesn't
work... what do I replace that with? Or how can I allow a module
access to the websocket? or is there a better way to accomplish this?

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
}
end
}
Reply all
Reply to author
Forward
0 new messages