[英]NodeJS Fork - React each time a newline is sent by subprocess
I am trying to write a function with NodeJS (on Windows7 if that makes a difference) that will listen to subprocess and, after each newline is sent via the subprocess, handle it in Node. 我正在尝试使用NodeJS(如果有区别,请在Windows7上)编写一个函数,该函数将侦听子流程,并在通过子流程发送了每个换行符后,在Node中进行处理。 Check out this example:
看看这个例子:
var express = require('express');
var app = express();
app.listen(3000);
app.get('/', function(request, response) {
var exec = require('child_process').exec,
child, out = "NA";
child = exec('java -jar misc.jar',
function (error, stdout, stderr) {
if(stdout!==''){
out = '---------stdout: ---------\n' + stdout;
}
if(stderr!==''){
out = '---------stderr: ---------\n' + stderr;
}
if (error !== null) {
out = '---------exec error: ---------\n[' + error+']';
}
response.send(out);
});
});
As you can imagine, the function (error, stdout, stderr)
only gets called after my subprocess is terminated. 可以想象,
function (error, stdout, stderr)
仅在子进程终止后才被调用。
Instead, I'd like that function to get called whenever a new line of text comes out of either stdout, stderr. 相反,我希望每当stdout,stderr中出现新的一行文本时,都调用该函数。 At that point, I'd like to use AJAX/Socket.IO to dynamically update the user-facing html page.
那时,我想使用AJAX / Socket.IO动态更新面向用户的html页面。 (Definitely bonus points if you can help point me in that direction as well!)
(如果您也可以帮助我指向该方向,则绝对可以加分!)
Okay I realized I need to use "spawn" and not "exec" to do this: 好的,我意识到我需要使用“ spawn”而不是“ exec”来做到这一点:
var express = require('express');
var app = express();
app.listen(3000);
app.get('/', function(request, response) {
response.send("Hey");
var spawn = require('child_process').spawn;
var java = spawn('java', ['-jar', 'misc.jar']);
java.stdout.on('data', function(data) {
console.log(data);
});
});
However, I notice that express with "response" is not allowing my to push data. 但是,我注意到带有“响应”的快速表示不允许我推送数据。 So, I need to figure out that last part of my question as I believe to do this I need to use AJAX/Socket.IO
因此,我需要弄清楚问题的最后一部分,因为我相信要这样做,我需要使用AJAX / Socket.IO
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.