如何刷新nodejs的child_process stdin.write

我需要在服务器端为客户端运行一个“C”程序。 这个程序可以互动。
我正在使用Node.js child_process类。 但是我在这里看到了一个问题:因为我需要保持程序交互,所以在客户端和node.js服务器之间会有消息交换。

1.程序说:inputx的值:
发送这个消息给客户端,并获得一些值作为input
2.当Nodejs服务器接收到来自客户端的input时,它执行child_process.stdin.write

但问题是程序不会执行,直到我标记stream的结束。 这可能是一个解决办法?
就像在用户可用时将值清空到程序一样。

更新:

test1.c
#包括

int main() { int x, y; printf("Enter x : "); fflush(stdout); scanf("%d", &x); printf("Enter y : "); fflush(stdout); scanf("%d", &y); printf("Value entered y is %d\n", y); printf("Value entered x is %d", x); } 

编译上面的代码来生成可执行文件a.out

 var spawn = require('child_process').spawn; var count = 0; var exec = spawn('./a.out'); exec.on('error', function (err){ console.log("Error" + err ); }); exec.stdout.on('data', function(data){ console.log("in stdout.on.data : " + data.toString()); if (count == 0) { exec.stdin.write('44\n'); count++; } else if (count == 1) { exec.stdin.write('54'); count++; } if (count == 2) { exec.stdin.end(); } }); exec.stderr.on('data', function(data) { console.log("in stderr.on.data : " + data); }); exec.on('close', function (code) { if (code != 0) { console.log("Program ended with a error code : " + code); } }); 

在上面的node.js代码中,如果我评论exec.stdin.end(); 它不起作用,并等待streamclosures。 我该如何解决这个问题? 因为我希望客户有一个持续的互动,所以我很难预测何时结束这个stream。

你忘了“按Enter”。

 exec.stdin.write('54' + "\n"); OR exec.stdin.write("54\n"); 

你也可以在JavaScript中查找一些关于语录的内容